[SOLVED] Player stops for a few seconds after pathfinding

local PFS = game:GetService("PathfindingService")
local ClickDetector = script.Parent
local endpart = script.Parent.Parent.Parent.EndPart

ClickDetector.MouseClick:Connect(function(plr)
	
	local char = plr.Character
	
	local Humanoid = char:WaitForChild("Humanoid")
	local Torso = char:WaitForChild("UpperTorso")
	
	local path = PFS:CreatePath()
	path:ComputeAsync(Torso.Position, endpart.Position)
	local waypoints = path:GetWaypoints()
	
	for i, Waypoint in pairs(waypoints) do
		
		Humanoid:MoveTo(Waypoint.Position)
		Humanoid.MoveToFinished:Wait()
        Humanoid.MoveToFinished:Connect(function()
			path:Destroy()
		end)
		
	end
	
end)

Hey!
So basically what you need to do is change this lines after the loop

Humanoid.MoveToFinished:Wait()
Humanoid.MoveToFinished:Connect(function()
			path:Destroy()
	end)

So the final Script should look like this

local PFS = game:GetService("PathfindingService")
local ClickDetector = script.Parent
local endpart = script.Parent.Parent.Parent.EndPart

ClickDetector.MouseClick:Connect(function(plr)

	local char = plr.Character

	local Humanoid = char:WaitForChild("Humanoid")
	local Torso = char:WaitForChild("UpperTorso")

	local path = PFS:CreatePath()
	path:ComputeAsync(Torso.Position, endpart.Position)
	local waypoints = path:GetWaypoints()

	for i, Waypoint in pairs(waypoints) do

		Humanoid:MoveTo(Waypoint.Position)

	end
	
	Humanoid.MoveToFinished:Wait()
	Humanoid.MoveToFinished:Connect(function()
		path:Destroy()
	end)
	
end)
1 Like

image
image