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
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
Tracks.Walk:Play(0)
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, Vector3.new(TargetRoot.Position.X, Root.Position.Y, TargetRoot.Position.Z))
local AttackRange = CardInfo.AttackRange
if TargetDistance <= AttackRange then
if AttackDebounce then return end
AttackDebounce = true
Tracks.Walk:Stop(0)
Tracks.Attack:Play(0)
TargetFigure.Humanoid:TakeDamage(CardInfo.Damage)
task.wait(1)
AttackDebounce = false
Tracks.Walk:Play(0)
else
if Pathfinding then return end
Pathfinding = true
local Path = S_PathfindingService:CreatePath(CardInfo.AgentParams)
local TargetPos = Vector3.new(TargetRoot.Position.X, 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
Humanoid:MoveTo(Waypoint.Position)
Humanoid.MoveToFinished:Wait()
end
Pathfinding = false
end
end
end)
(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).