I have made a pathfinding thingy when you click somewhere you go there, but if i click onto one place and the player is still moving and then i click onto other place then the loop does not break so the character first goes to waypoint of first path and then to waypoint of the second path and it happens always, i don't even have an idea of how to fix it, i tried, i failed, i'm lost, helppp pls thanks.

Script:

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()

local pfs = game:GetService("PathfindingService")

local path_parameters = {
	["AgentRadius"] = 4.5,
	["AgentHeight"] = 5,
	["AgentCanJump"] = false
}

if not player.Character then player.CharacterAppearanceLoaded:Wait()
end

local character = player.Character
local humanoid  = character:WaitForChild("Humanoid")

mouse.Button1Down:Connect(function()
	if mouse.Target ~= nil then
		local path = pfs:CreatePath(path_parameters)
		local destination = mouse.hit.p
		path:ComputeAsync(character:WaitForChild("HumanoidRootPart").Position, destination)
		local waypoints = path:GetWaypoints()
		for _, waypoint in pairs(waypoints) do
			humanoid:MoveTo(waypoint.Position)
			humanoid.MoveToFinished:Wait()
			print("Moving next!")
		end
	end
end)

3 comments

1

I think you may want to create a function for the pathfinding part so that when you do click again the loop breaks and the function is called again for the new path (if that's what you want.)

0

Yeah that is exactly what i want but im not sure how to implement it, but thanks that helped me too.

0

@Xapelize eat alcohol