So I finally got an NPC that runs away from the closest player, but there is only one problem. The script seems to break after a while. I was chasing it around with one player towards the other player, and it was working fine. But after a bit, it just stopped. You could say it got scared out of it's mind, but I'd like to fix this.
Closest Player Script:
wait(1)
repeat wait(0.5) until (game.Players.NumPlayers >= 1)
while true do
local smallest = {math.huge,nil}
for _,v in pairs(game.Players:GetPlayers()) do
local cur = v:DistanceFromCharacter(script.Parent.Parent.Torso.Position)
if cur < smallest[1] then
smallest[1] = cur
smallest[2] = v
end
end
script.Parent.Value = smallest[2].Name
wait()
end
--//
Run Script:
local phase = script.Parent.Phase
local h = script.Parent.Humanoid
local name = script.Parent.Target
wait(10)
while true do
if name.Value ~= "" then
local plr = game.Workspace:FindFirstChild(name.Value)
if phase.Value == "Run" then
tor = script.Parent.Torso
tar = plr.Torso
local newCF = CFrame.new(tor.Position,tar.Position)
local direction = newCF.lookVector * -1
h:Move(direction)
end
end
wait()
end
--//
Errors:
20:18:45.173 - New connection from 127.0.0.1|57615
20:18:45.242 - Replication: Can't create default object of type Players
Player -1 added
20:18:46.436 - New connection from 127.0.0.1|65210
20:18:46.448 - Replication: Can't create default object of type Players
Player -2 added
~MightyDantheman |