function findNearestTorso(pos) local dist = 2048 local list = workspace:GetChildren() local torso = nil local temp = nil local human = nil local temp2 = nil if list ~= nil then for x = 1, #list do temp2 = list[x] if (temp2.className == "Model") and (temp2 ~= script.Parent) and (temp2:FindFirstChildOfClass("LocalScript")) then temp = temp2:findFirstChild("HumanoidRootPart") human = temp2:findFirstChildOfClass("Humanoid") if (temp ~= nil) and (human ~= nil) and (human.Health > 0) then if (temp.Position - pos).magnitude < dist then torso = temp dist = (temp.Position - pos).magnitude end end end end return torso end end while wait(1) do target = findNearestTorso(script.Parent.HumanoidRootPart.Position) x = math.random(1, 10) if x > 2 then if target ~= nil then script.Parent.Humanoid:MoveTo(target.Position) end else script.Parent:findFirstChildOfClass("Humanoid"):MoveTo(script.Parent.HumanoidRootPart.Position + Vector3.new(math.random(-10, 10), script.Parent.HumanoidRootPart.Position.Y, math.random(-10, 10))) end end
This is a script i got from a friend that makes it so the enemy follows the player but for some reason it doesn't work how do i fix this?
ok i fixed it myself you are supposed to unanchor the humanoidrootpart which is set to true when you insert :/