How to timeout a moveToFinished?

I’m making a piggy style game, and I have an AI script I made myself, but the monster completes its entire path before updating and re-routing to track the player again.

I’ve tried everything I could think of, but I can’t get it to work no matter what I do.

How would I timeout a MoveToFinished?

Here’s the pathdfinding Code -

local function walkTo(target)
	local path = getPath(target)
	if path.Status == Enum.PathStatus.Success then
		for index, waypoint in pairs(path:GetWaypoints()) do
			if waypoint.Action == Enum.PathWaypointAction.Jump then
				humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
			end
			if target:FindFirstChild("Humanoid") and target:FindFirstChild("Humanoid").Health > 0 then
				if CanSeeTarget(target) then
					humanoid:MoveTo(target.HumanoidRootPart.Position)
					sunny.Eyes.Color = sunny.GlowingEye.Color
					sunny.Eyes.Material = Enum.Material.Neon
					break
				end
				humanoid:MoveTo(waypoint.Position)
				sunny.Eyes.Material = eyeMaterial
				sunny.Eyes.Color = eyeColour
				humanoid.MoveToFinished:Wait()
			end
		end
	end
end	
local function patrol()
	local target = findTarget()
	if target then
		walkTo(target)
		target = findTarget()
	end
end

while true do
	patrol()
end

I believe (just like WaitForChild) that MoveToFinished can be provided with a number argument, which would be a timeout. Try doing :MoveToFinished(x) where x is the amount of seconds until it times out.

I’ve tried that, it doesn’t seem to have any effect :confused:
And if it did, it would just start moving to the next waypoint and that would mess it up

You could create your own timeout handler by connecting to MoveToFinished then just waiting for x seconds, and if something went wrong then do something.

I admit though that i am not very qualified to answer NPC related things, so i will not waste your time. If anyone else has something then please contribute!

1 Like