So I wrote this code to follow the closet Player but… It doesn’t work and I’m not sure how to fix it Here is the code.
local rarm = script.Parent:FindFirstChild("HumanoidRootPart")
local PathfindingService = game:GetService("PathfindingService")
function findNearestTorso(pos)
local list = game.Workspace:children()
local torso = nil
local dist = 100000000
local temp = nil
local human = nil
local temp2 = nil
for x = 1, #list do
temp2 = list[x]
if (temp2.className == "Model") and (temp2 ~= script.Parent) then
temp = temp2:findFirstChild("HumanoidRootPart")
human = temp2:findFirstChild("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
while true do
wait(math.random(1,1))
local target = findNearestTorso(script.Parent.HumanoidRootPart.Position)
if target ~= nil then
local path = PathfindingService:CreatePath()
-- Get the path waypoints
path:ComputeAsync(target.Position,Temp.HumanoidRootPart)
local waypoints = path:GetWaypoints()
-- Loop through waypoints
for _, waypoint in pairs(waypoints) do
local part = Instance.new("Part")
part.Shape = "Ball"
part.Material = "Neon"
part.Size = Vector3.new(0.6, 0.6, 0.6)
part.Position = waypoint.Position
part.Anchored = true
part.CanCollide = false
part.Parent = game.Workspace
script.Parent.Humanoid:MoveTo(waypoints.Position)
script.Parent.Humanoid.MoveToFinished:Wait()
end
end
end
local PathfindingService = game:GetService("PathfindingService")
function findNearestTorso(pos)
local list = game.Workspace:children()
local torso = nil
local dist = 100000000
local temp = nil
local human = nil
local temp2 = nil
for x = 1, #list do
temp2 = list[x]
if (temp2.className == "Model") and (temp2 ~= script.Parent) then
temp = temp2:findFirstChild("HumanoidRootPart")
human = temp2:findFirstChild("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
while true do
wait(math.random(1,1))
local target = findNearestTorso(script.Parent.HumanoidRootPart.Position)
if target ~= nil then
local path = PathfindingService:CreatePath()
-- Get the path waypoints
path:ComputeAsync(script.Parent.HumanoidRootPart.Position,target.Position)
local waypoints = path:GetWaypoints()
-- Loop through waypoints
for _, waypoint in pairs(waypoints) do
local part = Instance.new("Part")
part.Shape = "Ball"
part.Material = "Neon"
part.Size = Vector3.new(0.6, 0.6, 0.6)
part.Position = waypoint.Position
part.Anchored = true
part.CanCollide = false
part.Parent = game.Workspace
script.Parent.Humanoid:MoveTo(target.Position)
end
end
end