Requesting assistance with PathfindingService

Greetings! I’m here to ask for assistance once more. I’ve recently figured out how to stop pathways and creates new ones (as before I had difficulties changing paths). Now that I’ve figured out a way I’ve encountered an issue where it gets buggy and I’d assume the reason is because of the Humanoid.MoveToFinished:Wait() and that waits for at least 1 point to finish.

Mouse.Button2Down:Connect(function()
	local Target = Mouse.Target
	local Character = Player.Character
	
	if Character and Character.PrimaryPart then
		if Target:IsDescendantOf(workspace.World.mapAssets) then
			visualRing() -- Visuals
			
			local path = PathfindingService:CreatePath()
			path:ComputeAsync(Character.PrimaryPart.Position, Mouse.Hit.Position)
			local waypoints = path:GetWaypoints()
			updatePath:Fire()
			local blockedPathEvent
			
			blockedPathEvent = path.Blocked:Connect(function()
				blockedPathEvent:Disconnect()
				updatePath:Fire() -- Basically stops the movement
			end)
			
			spawn(function()
				local stop = false
				local listener 
				
				listener = updatePath.Event:Connect(function()
					listener:Disconnect()
					stop = true
					for _, point in Pairs(workspace.visualAssets.pathPoints:GetChildren()) do
						point:Destroy()
					end
				end)
				
				generateVisualPath(waypoints)
				
				for index, waypoint in Pairs(waypoints) do	
					if stop then
						break
					end
					Character.Humanoid:MoveTo(waypoint.Position)
					Character.Humanoid.MoveToFinished:Wait()
				end
			end)
		end
	end
end)

Here’s what it does:
https://gyazo.com/7c6d134acb5c403d2fa16abc52f37942

As you can see it glitches a lot in rotation and positioning. So my question would be how would I be able to instantly break that loop and proceed to a new path. Anyways to improve this script would be appreciated. I removed most of the irrelevant code to keep this simple.

Thanks for any feedback or support!


3 Likes

I think this belongs in #help-and-feedback:code-review because you are asking for improvements

I am asking for improvements but my main issue still stands, I just threw in the last line in case the improvements may fix the error or bug (mainly looking for others methods / ways to do this if that will solve the issue)

1 Like

Try using the :MoveTo() on the humanoid before moving them towards the new path. For example, if I clicked on a new path for the character to walk to. I would first do Character.Humanoid:MoveTo(Character.HumanoidRootPart.Position) so it would essentially cancel out the MoveTo() because it is already in that position. After that, I would then set the MoveTo() to the point where the player pressed. I hope I explained well.

I don’t understand could you show me in code?

Mouse.Button2Down:Connect(function()
local Target = Mouse.Target
local Character = Player.Character

if Character and Character.PrimaryPart then
	if Target:IsDescendantOf(workspace.World.mapAssets) then
		visualRing() -- Visuals
		
		local path = PathfindingService:CreatePath()
		path:ComputeAsync(Character.PrimaryPart.Position, Mouse.Hit.Position)
		local waypoints = path:GetWaypoints()
		updatePath:Fire()
		local blockedPathEvent
		
		blockedPathEvent = path.Blocked:Connect(function()
			blockedPathEvent:Disconnect()
			updatePath:Fire() -- Basically stops the movement
		end)
		
		spawn(function()
			local stop = false
			local listener 
			
			listener = updatePath.Event:Connect(function()
				listener:Disconnect()
				stop = true
				for _, point in Pairs(workspace.visualAssets.pathPoints:GetChildren()) do
					point:Destroy()
				end
			end)
			
			generateVisualPath(waypoints)
			Character.Humanoid:MoveTo(Character.HumanoidRootPart.Position) -- This will basically cancel out the MoveToFinished() 
			for index, waypoint in Pairs(waypoints) do	
				if stop then
					break
				end
				Character.Humanoid:MoveTo(waypoint.Position)
				Character.Humanoid.MoveToFinished:Wait()
			end
		end)
	end
end
end)

I just added that one line after generateVisualPath(). Try it out.

1 Like

I am certainly amazed how you think outside of the box but unfortunately:

https://gyazo.com/016f0323f3b85aa1ecd224c6028c7789

Thanks a lot for trying

Okay. Well lets go a little more old school. Try setting the CFrame of the HumanoidRootPart to the CFrame that its at. For example,

 Character.HumanoidRootPart.CFrame = Character.HumanoidRootPart.CFrame 

Try putting that on the line after generateVisualPath(). If you still have the MoveTo from what I tried. Remove that and just try this.

I like where you are going but now you are going off a little, the good thing is, using your previous method Character.Humanoid:MoveTo(Character.HumanoidRootPart.Position) is the solution, the only thing is that you positioned it in the wrong area. What I did is place it inside the listener. Anyway thanks for your help. You truly have thought outside of the box and helped.

Oh. I was thinking about that too, but I thought it would end up making it worse. Guess not! I learned a few things today. :slight_smile: (If I could go on Studio right now I would’ve test and gave you a more in-depth answer.)

1 Like