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

My Pathfinding script is malfunctioning?

I figured out how to use pathfinding on a higher platform, involving raycasting and magnitude (from a youtube video) and I converted it into a tool. The problem is that for no reason of which I can see, is that when clicked, on random occasions it goes backward for a second and gets back on track. It's very annoying for me (a perfectionist). Is there a way to fix it?

Video of what is happening

Inside of a localscript, inside a tool

--These are to disable player's controls--
local ContextActionService = game:GetService("ContextActionService")
local FREEZE_ACTION = "freezeMovement"

--general defining
local enabled = true
local escape = false
local tool = script.Parent
local player = game.Players.LocalPlayer
local char = game.Workspace:WaitForChild(player.Name)
local mouse = game.Players.LocalPlayer:GetMouse()


if not game.Workspace:FindFirstChild("PathfindingFor"..player.Name) then
	folder = Instance.new("Folder",game.Workspace)
	folder.Name = "PathfindingFor"..player.Name
	visualMarks = Instance.new("Folder",folder)
	visualMarks.Name = "VisualMarks"
end

local rootPart = char:WaitForChild("HumanoidRootPart")
local human = char:WaitForChild("Humanoid")

function findTarget(pos)
	local objective = game.Players.LocalPlayer:GetMouse().Hit.p
	local visualPoint = Instance.new("Part",folder)
	if game.Workspace:WaitForChild("PathfindingFor"..player.Name):FindFirstChild("VisualPointFor"..player.Name) then
		game.Workspace:WaitForChild("PathfindingFor"..player.Name):FindFirstChild("VisualPointFor"..player.Name):Destroy()
	end
	visualPoint.Name = "VisualPointFor"..player.Name
	visualPoint.Size = Vector3.new(2,2,2)
	visualPoint.Shape =  "Ball"
	visualPoint.Anchored = true
	visualPoint.BrickColor = BrickColor.new("Bright red")
	visualPoint.Position = game.Players.LocalPlayer:GetMouse().Hit.p
	visualPoint.CanCollide = false
	visualPoint.Material = Enum.Material.Neon
	visualPoint.Transparency = 0.4
	visualPoint.BackSurface = Enum.SurfaceType.SmoothNoOutlines
	visualPoint.FrontSurface = Enum.SurfaceType.SmoothNoOutlines
	visualPoint.BottomSurface = Enum.SurfaceType.SmoothNoOutlines
	visualPoint.TopSurface = Enum.SurfaceType.SmoothNoOutlines
	visualPoint.LeftSurface = Enum.SurfaceType.SmoothNoOutlines
	visualPoint.RightSurface = Enum.SurfaceType.SmoothNoOutlines
	
	local dist = 1000
	local target = nil
	for i,v in pairs(game.Workspace:GetChildren()) do
			if (objective - rootPart.Position).magnitude < dist then
				dist = (objective - rootPart.Position).magnitude
				target = objective
			end
	end
	return target
end

function part()
	ContextActionService:BindAction(
    FREEZE_ACTION,
    function()
        return Enum.ContextActionResult.Sink
    end,
    false,
    unpack(Enum.PlayerActions:GetEnumItems())
		)
	enabled = false
	if game.Workspace:WaitForChild("PathfindingFor"..player.Name):WaitForChild("VisualMarks"):FindFirstChild("VisualMarkFor"..player.Name) then
					for i,v in pairs (game.Workspace:WaitForChild("PathfindingFor"..player.Name):WaitForChild("VisualMarks"):GetChildren()) do
						v:Destroy()
					end
				end
	local head = findTarget()
	
	if head then
		local ray = Ray.new(rootPart.Position, (head - rootPart.Position).Unit * 200)
		
		if head then
			
				human:MoveTo(head)
				wait(0.4)
				local path = game:GetService("PathfindingService"):FindPathAsync(rootPart.Position,head)
				local points = path:GetWaypoints()
				for i,v in pairs (points) do
					
					local VisualMark = Instance.new("Part",visualMarks)
			VisualMark.Name = "VisualMarkFor"..player.Name
			VisualMark.Size = Vector3.new(1,1,1)
			VisualMark.Shape =  "Ball"
			VisualMark.Anchored = true
			VisualMark.BrickColor = BrickColor.new("Bright blue")
			VisualMark.Position = v.Position
			VisualMark.CanCollide = false
			VisualMark.Material = Enum.Material.Neon
			VisualMark.Transparency = 0.6
			VisualMark.BackSurface = Enum.SurfaceType.SmoothNoOutlines
			VisualMark.FrontSurface = Enum.SurfaceType.SmoothNoOutlines
			VisualMark.BottomSurface = Enum.SurfaceType.SmoothNoOutlines
			VisualMark.TopSurface = Enum.SurfaceType.SmoothNoOutlines
			VisualMark.LeftSurface = Enum.SurfaceType.SmoothNoOutlines
			VisualMark.RightSurface = Enum.SurfaceType.SmoothNoOutlines
			local InvisiblePoint = game.ReplicatedStorage.Checkpoint:Clone()
			InvisiblePoint.Parent = VisualMark
			InvisiblePoint.Position = VisualMark.Position
				end
				if path.Status == Enum.PathStatus.Success then
					
				
				for i,v in pairs (points) do
					if escape ~= false then
				escape = false
				if game.Workspace:WaitForChild("PathfindingFor"..player.Name):FindFirstChild("VisualPointFor"..player.Name) then 
					game.Workspace:WaitForChild("PathfindingFor"..player.Name)["VisualPointFor"..player.Name]:Destroy()
				end
				if game.Workspace:WaitForChild("PathfindingFor"..player.Name):WaitForChild("VisualMarks"):FindFirstChild("VisualMarkFor"..player.Name) then
					for i,v in pairs (game.Workspace:WaitForChild("PathfindingFor"..player.Name):WaitForChild("VisualMarks"):GetChildren()) do
						v:Destroy()
					end
				end
				
				break
			end
					human:MoveTo(v.Position)
					human.MoveToFinished:Wait()
					if v.Action == Enum.PathWaypointAction.Jump then
						human.Jump = true
					end
					if (points[#points].Position - head).magnitude > 15 then
						break
					end
				end
				else
					--human:MoveTo(head)
					warn("Path Status: Failed.")
				end
			end
	end
	ContextActionService:UnbindAction(FREEZE_ACTION)
	enabled = true
	if game.Workspace:WaitForChild("PathfindingFor"..player.Name):WaitForChild("VisualMarks"):FindFirstChild("VisualMarkFor"..player.Name) then
					for i,v in pairs (game.Workspace:WaitForChild("PathfindingFor"..player.Name):WaitForChild("VisualMarks"):GetChildren()) do
						v:Destroy()
					end
				end
end
script.Parent.Equipped:connect(function()
    script.Parent.Activated:connect(function()
	if enabled ~= false then
		
        part(mouse.Hit)
	end
    end)
end)
mouse.KeyDown:connect(function(key)
    if key == "e" then
       escape = true
    end
end)

1 answer

Cut off the first node you use from pathfinding service, almost always the rest of the path is still accessible, as it tends to put points really close together.

Pathfinding service idealizes some things so it doesn't take forever to get a path, so instead of using an actual position of -3.57882 or something similar, it rounds it to either 3 or 4, and marks that as the start point, so you might walk backwards to -4, then forwards to your target at 0.

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