MoveTo Fires instantly

I’m trying to make an “enemy pathfinding” system

that for some reason the .MoveToFinished:Wait() event fires instantly, also try TweenService.Completed:Wait() to make the character move but the character doesn’t wait either.

I’m too stressed I’ve been trying to understand what’s wrong for a long time, everything used to work perfectly, but I did something :weary:

The .Heartbeat function is only one, already verify that it is only one.

The variable called “Pathfinding” it’s supposed to be a Debounce so it doesn’t start another pathfinding while moving, also the even called “FindNearestTarget” just returns the nearest target, thats not the problem

local Humanoid = Figure.Humanoid
local Root = Figure.HumanoidRootPart
local AttackDebounce = false
local Pathfinding = false
local CurrentTarget 
local CheckLoopFunction = true

CheckLoopFunction = S_RunService.Heartbeat:Connect(function(DeltaTime)
	if Humanoid.Health <= 0 then DeadTroop(Figure) CheckLoopFunction:Disconnect() end

	local TargetRoot, TargetFigure, Type, TargetDistance = FindNearestTarget(CardInfo.DetectRange, Figure, TeamFolder, CanDetectTroops)

	CurrentTarget = TargetFigure

	if TargetRoot then
		Root.CFrame = CFrame.lookAt(Root.Position,, Root.Position.Y, TargetRoot.Position.Z))

		local AttackRange = CardInfo.AttackRange

		if TargetDistance <= AttackRange then
			if AttackDebounce then return end
			AttackDebounce = true

			AttackDebounce = false
			if Pathfinding then	return end
			Pathfinding = true

			local Path = S_PathfindingService:CreatePath(CardInfo.AgentParams)

			local TargetPos =, Root.Position.Y, TargetRoot.Position.Z)
			Path:ComputeAsync(Root.Position, TargetPos)
			for Index, Waypoint in pairs(Path:GetWaypoints()) do
				if CurrentTarget ~= TargetFigure or AttackDebounce then break end

			Pathfinding = false

(I guess I’m too stressed to realize that’s the problem, lol. It’s probably something very simple that I can’t figure out what it is).

I recommend using MoveToFinished:Connect() instead or using a boolean to toggle. Cause that’s what I used for my 2008 Roblox game.

I can’t help but, just for you to know, the Tween.CompletedWait() is kinda tricky to use. I suggest using Twen.Completed:Connect or MoveToFinished:Connect()

When an Event is Fired, It doesnt fire in the exact same thread, Hearbeat runs every 1/60 of a second depending on the FPS you have, and every 1/60 of a second, is when it will fire, Since it doesnt yield the main thread, you are essentially telling it to yield for the current thread is in, its working, but because the code for the connection is fired somewhere else, it wont effect the main thread, therefore firing instantly everytime.

A simple fix can be to check everytime to be sure that they have this connection, usually by if statement.

I’m so confused, right now…
The cause of all this problem was this line of code:

Root.CFrame = CFrame.lookAt(Root.Position,, Root.Position.Y, TargetRoot.Position.Z))


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