MoveToFinished not Finishing

Hi. I’m following this tds game tutorial and I have run into an issue. If the monster moves too slowly, they will just give up on moving to the next waypoint and skip one. This doesn’t happen to faster monsters. I have looked up the issue and came across an 8 second timeout, where an npc will timeout the MoveTo() function after 8 seconds. I saw some fixes but I just have no idea how to implement them into the code that I have gotten from the tutorial.
Script:

function mob.Move(mob, map)
	local hum = mob:WaitForChild("Humanoid")
	local waypoints = map.Waypoints

	for waypoint = 1, #waypoints:GetChildren() do
		hum:MoveTo(waypoints[waypoint].Position)
		hum.MoveToFinished:Wait()
	end
	
	mob:Destroy()
	
end

this should work:

local function moveTo(humanoid, targetPoint)
	local targetReached = false

	local connection
	connection = humanoid.MoveToFinished:Connect(function(reached)
		targetReached = true
		connection:Disconnect()
		connection = nil
	end)
	humanoid:MoveTo(targetPoint)

	task.spawn(function()
		while not targetReached do
			if not (humanoid and humanoid.Parent) then
				break
			end
			if humanoid.WalkToPoint ~= targetPoint then
				break
			end
			humanoid:MoveTo(targetPoint)
			task.wait(6)
		end
		if connection then
			connection:Disconnect()
			connection = nil
		end
	end)
end

function mob.Move(mob, map)
	local hum = mob:WaitForChild("Humanoid")
	local waypoints = map.Waypoints

	for waypoint = 1, #waypoints:GetChildren() do
		moveTo(hum, waypoints[waypoint].Position)
		hum.MoveToFinished:Wait()
	end

	mob:Destroy()
end

the moveTo function taken from the roblox docs (Humanoid | Documentation - Roblox Creator Hub)

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.