Having trouble with road-based pathfinding system

  1. What do you want to achieve? I want to make a pathfinding/waypoint system, one that follows along the roads that i have preset inside my game so that the player can follow it when in a car.

  2. What is the issue? I’m having trouble making it follow the road only, as I do not know what to do to make it prefer using roads over just any walkable place.

  3. What solutions have you tried so far? I tried creating a PathfindingLink labelled as “RoadPoints” that I thought would run from one end of the road, to the other and when the player was near it, I thought it would just link the player to it and continue, but it turns out I was wrong and the pathfinding just ignores the links.

Here is my local script:

Basically, it recieves an event and then changes the “SpecialWP” value to the part. Then the constantly running “RenderStepped” function notices the change and continously updates the pathfinding so that the player is shown a straight path towards the “SpecialWP”.

local pathParts = game.Workspace.WaypointService

local Path = game:GetService('PathfindingService')

local SpecialWP;
local MapWP;

local Player = game.Players.LocalPlayer

game.ReplicatedStorage.Remotes.FindWaypoint.OnClientEvent:Connect(function(wptype, part)
	if wptype == 'Special' then
		if SpecialWP ~= false then
			SpecialWP = part 
		else
			SpecialWP = nil
		end
	elseif wptype == 'Map' then
		if MapWP ~= false then
			MapWP = part 
		else
			MapWP = nil
		end	
	end	
end)

local Run = game:GetService('RunService')

local Debounce = false
Run.RenderStepped:Connect(function()
	if Debounce == false then
		Debounce = true
		if SpecialWP ~= nil then
			
			local Wayfinder = Path:CreatePath({
				AgentRadius = 4;
				AgentHeight = 16;
				AgentCanJump = true;	
				Costs = {
					RoadPoints = 1;
					
				}
			})
			Wayfinder:ComputeAsync(Player.Character.LowerTorso.Position, SpecialWP.Position)

			local Waypoints = Wayfinder:GetWaypoints()
			local LastWPPosition;
			
			for i, PrevPoint in pairs(game.Workspace.WaypointService:GetChildren()) do
				if PrevPoint.Name == 'SpecialWP' then
					PrevPoint:Destroy()
				end
			end
			
			for i, Point in pairs(Waypoints) do
				LastWPPosition = Point.Position
				local Part = Instance.new('Part')
				Part.Color = Color3.fromRGB(29, 255, 243)
				Part.Name = 'SpecialWP'
				Part.Parent = game.Workspace.WaypointService
				Part.Shape = Enum.PartType.Ball
				Part.Anchored = true
				Part.CanCollide = true
				Part.Position = Point.Position
				Part.Size =  Vector3.new(.75, .75, .75)
				Part.Material = Enum.Material.Neon
			end
		end
		
		if MapWP ~= nil then
			
		end
		wait(1.5)
		Debounce = false
	end
end)

Feel free to request for further details that would help you help me find a solution to my problem!

1 Like

Hi! Late reply but, Only thing I can think off is pathfinding modifiers, Hopefully you figured it out. However there may be other ways to make them follow roads. Who knows, Please correct me if I’m misunderstanding

I had already figured it out, thanks anyways!