MoveTo stopping midway through

I’m making this “Steal a” game. While coding the unicorns, i used a MoveTo() function to move them across the path. But for some reason, they stop at the exact same spot every single time. How do i fix this?

Here’s the code handling the unicorns:

function unicorn:MoveAlongDefaultPath()
local endPoint = waypoints.EndPoint
if not endPoint then warn(“no endpoint!!!”) return end

local humanoid: Humanoid = self.Clone:FindFirstChildWhichIsA("Humanoid")
if humanoid then
	self.Conns.MoveConn = humanoid.MoveToFinished:Connect(function(reached)
		print("Finished moveto.")
		if reached then
			self:Destroy()
		end
	end)
	
	humanoid:MoveTo(endPoint.Position + Vector3.new(0, 5, 0))
else
	warn("No humanoid found in unicorn model")
end

end

if Humanoid:MoveTo() takes too long (8 seconds I believe), it will stop.

Upon Humanoid.MoveToFinished, use the ‘Reached’ variable to determine if you need to run the code again.

Here is how I would do it.

local function MoveTo(Humanoid : Humanoid, Position : Vector3)
	if Humanoid then
		Humanoid:MoveTo(Position)
	end
end

local humanoid: Humanoid = self.Clone:FindFirstChildWhichIsA("Humanoid")
local Goal = endPoint.Position + Vector3.new(0, 5, 0)

if humanoid then
	self.Conns.MoveConn = humanoid.MoveToFinished:Connect(function(reached)
		print("Finished moveto.")
		if reached then
			self:Destroy()
		else
			MoveTo(humanoid, Goal)
		end
	end)
	
	MoveTo(humanoid, Goal)
else
	warn("No humanoid found in unicorn model")
end

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