The red spheres circled in red are the waypoints and the little orange guy circled in green (might be kinda hard to see) is the NPC where he stops every time, roughly 1 3rd or 1 4th of the way there
(P.S. I don’t believe there are any errors)
Any and all help is appreciated, thanks in advance!
Not the best script I’ve ever seen, but you should be using a for loop.
Here’s your new script, I’m happy to explain anything if you don’t understand. (I’ve kept it in the original state of predetermined waypoints in case you need it like this)
local npc = script.Parent
local humanoid = npc:WaitForChild("Humanoid")
local waypoints = game.Workspace:WaitForChild("Waypoints")
local function moveToWaypoint(waypoint)
humanoid:MoveTo(waypoint.Position)
humanoid.MoveToFinished:Wait()
wait(0.1)
end
game.ReplicatedStorage.Attack.Event:Connect(function()
moveToWaypoint(waypoints.W1)
moveToWaypoint(waypoints.W2)
moveToWaypoint(waypoints.W3)
moveToWaypoint(waypoints.W4)
print("Movement Finished!")
end)
It says:
" The reach goal state of a humanoid will timeout after 8 seconds if it doesn’t reach its goal. This is done so that NPCs won’t get stuck waiting for Humanoid.MoveToFinished to fire. If you don’t want this to happen, you should repeatedly call MoveTo so that the timeout will keep resetting."
This seems like it may be the issue but I don’t know how to repeatedly call MoveTo
You could just use the function in the sample code from the documentation so your code would look something like this:
local npc = script.Parent
local humanoid = npc.Humanoid
local waypoints = game.Workspace.Waypoints
local waypointList={waypoints.W1, waypoints.W2, waypoints.W3, waypoints.W4}
-- I've created a table of your waypoints to make the code easier to read. You can simply add more waypoints to the list if required.
local function moveTo(humanoid, targetPoint) -- This is the function from the sample code
local targetReached = false
-- listen for the humanoid reaching its target
local connection
connection = humanoid.MoveToFinished:Connect(function(reached)
targetReached = true
connection:Disconnect()
connection = nil
end)
-- start walking
humanoid:MoveTo(targetPoint)
-- execute on a new thread so as to not yield function
task.spawn(function()
while not targetReached do
-- does the humanoid still exist?
if not (humanoid and humanoid.Parent) then
break
end
-- has the target changed?
if humanoid.WalkToPoint ~= targetPoint then
break
end
-- refresh the timeout
humanoid:MoveTo(targetPoint)
task.wait(6)
end
-- disconnect the connection if it is still connected
if connection then
connection:Disconnect()
connection = nil
end
end)
end
game.ReplicatedStorage.Attack.Event:Connect(function()
for _, wp in ipairs(waypointList) do -- loop through the waypoint list
task.wait(0.1)
moveTo(humanoid, wp.Position) -- using the custom moveTo function that doesn't timeout
humanoid.MoveToFinished:Wait()
end
print("success")
end)
Ahh my bad - forgot to include humanoid.MoveToFinished:Wait() after calling moveTo so edit your code:
game.ReplicatedStorage.Attack.Event:Connect(function()
for _, wp in ipairs(waypointList) do -- loop through the waypoint list
task.wait(0.1)
moveTo(humanoid, wp.Position) -- using the custom moveTo function that doesn't timeout
humanoid.MoveToFinished:Wait()
end
print("success")
end)