Hey. I’ve been using pathfinding recently, but a small detailed blundered me…
I have a function to calculate a new path when I run it. However, when I create a new path before the previous one finished, it gets really “wonky”. It seems the character begins to move back and forth between waypoints. Is there a way to solve this?
local function newPath(v,finish,o)
spawn(function()
if moving then
cancel = true
repeat wait() until moving == false
end
path:ComputeAsync(script.Parent.HumanoidRootPart.Position, v)
waypoints = path:GetWaypoints()
moving = true
for _, waypoint in pairs(waypoints) do--for waypoint
if waypoint.Action == Enum.PathWaypointAction.Jump then--nested if
if humanoid:GetState() ~= Enum.HumanoidStateType.Jumping and humanoid:GetState() ~= Enum.HumanoidStateType.FallingDown and humanoid:GetState() ~= Enum.HumanoidStateType.Freefall then
humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
end
end--end nested if
if cancel == true then
moving = false
cancel = false
break
end
humanoid:MoveTo(waypoint.Position )
humanoid.MoveToFinished:Wait()
end
moving = false
if finish then
finish()
end
end)
i don’t actally see anything wrong with your script at the current moment, but your script is a bit overcomplicated, so ive simplified it, im not sure if this will work for you though.
Script
local waypoints = {}
local function newPath(v,finish,o)
waypoints = {}
local path = game:GetService("PathfindingService"):CreatePath():ComputeAsync(script.Parent.HumanoidRootPart.Position, v)
waypoints = path:GetWaypoints()
for _, waypoint in pairs(waypoints) do
if waypoint.Action == Enum.PathWaypointAction.Jump then
if humanoid:GetState() ~= Enum.HumanoidStateType.Jumping and humanoid:GetState() ~= Enum.HumanoidStateType.FallingDown and humanoid:GetState() ~= Enum.HumanoidStateType.Freefall then
humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
end
end
humanoid:MoveTo(waypoint.Position )
humanoid.MoveToFinished:Wait()
end
if finish then
finish()
end
end
Well what I tried there is to break once the cancel variable is true. However, none of it seems to be working at the moment(Moving is used to detect if the character is moving or not, canceled is the variable to shut down the movement). Is there a simpler method to achieve this?