Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

script causes path spamming?

so I was looking the the path finding tutorial, and trying to make it to when you press right mouse button, a path is created and your character follows the way points of the path.

Currently if you spam right mouse button, tons of paths are created and the character slowly follows every single one.

How would I make it to where when you create a path, the old one is deleted? This is the script (without the hover script, but you can replace hover with mouse.Hit I think)

local destination = nil
Mouse.Button2Down:connect(function() --Moves character to target position
    if Mouse.Target~=nil and Hover.Transparency==0 then
		destination = Hover
		local path = PathfindingService:CreatePath()
		
		path:ComputeAsync(Char.HumanoidRootPart.Position, Hover.Position)
		
		local waypoints = path:GetWaypoints()
		
		for _, waypoint in pairs(waypoints) do
			local part = Instance.new("Part")
			part.Shape = "Ball"
			part.Material = "Neon"
			part.Size = Vector3.new(0.6, 0.6, 0.6)
			part.Position = waypoint.Position
			part.Anchored = true
			part.CanCollide = false
			part.Parent = game.Workspace
		
		Char.Humanoid:MoveTo(waypoint.Position)
		Char.Humanoid.MoveToFinished:Wait()
		end
	end
end)

2 comments

0

This doesn't fix your problem, but you could add a debounce to it ?

0

well that would make it to where you can only create a path every few seconds

1 answer

Try this

local destination = nil
local path --creates a nil variable

Mouse.Button2Down:connect(function() --Moves character to target position
	if Mouse.Target~=nil and Hover.Transparency==0 then
		destination = Hover

		if path then --If there's a current path then
			path = nil --Make it nil
		end
		
		path = PathfindingService:CreatePath()
		path:ComputeAsync(Char.HumanoidRootPart.Position, Hover.Position)

		local waypoints = path:GetWaypoints()

		for _, waypoint in pairs(waypoints) do
		
			if not path then --If the path is nil then
				break --ends the loop
			end
			
			local part = Instance.new("Part")
			part.Shape = "Ball"
			part.Material = "Neon"
			part.Size = Vector3.new(0.6, 0.6, 0.6)
			part.Position = waypoint.Position
			part.Anchored = true
			part.CanCollide = false
			part.Parent = game.Workspace

			Char.Humanoid:MoveTo(waypoint.Position)
			Char.Humanoid.MoveToFinished:Wait()
		end
	end
end)

1 comment

0

sadly it doesn't seem to work, you can still spam right click and create a ton of paths

This discussion is preserved as it was. New answers, comments, and votes are closed.