"Part Based" Pathfinding

Greetings! i need help to make a “part based” pathfinding system for my units.
These are my current “parts”:


And this is my workspace:
image
I already did setup a RemoteEvent and a pretty basic script, but i got stuck at the pathfinding part.

Server Script:

--Variables

local PathFindingService = game:GetService("PathfindingService")

local ReplicatedStorage = game.ReplicatedStorage
local RemoteEvent = ReplicatedStorage.RemoteEvents.Units.UnitMove

--//Move Unit to Province 
local function MoveUnit(Player, TAG, Target, Units)
	
end

RemoteEvent.OnServerEvent:Connect(MoveUnit)

Client Script:

--Variables
local Player = game:GetService("Players").LocalPlayer
local Mouse = Player.GetMouse()
local TAG = Player.TAG

local ReplicateStorage = game.ReplicatedStorage
local RemoteEvent = ReplicateStorage.RemoteEvents.Units.UnitMove

--Script

--//Call RemoteEvent When Right Click

local function RightClick(Player)
	
	local Target = Mouse.Target
	for i,v in pairs(game.Workspace.Units:GetChildren()) do
		if v.Info.isSelected == true then
			RemoteEvent:FireServer(TAG.Value ,Target, v)
		end
	end
end

Mouse.Button2Down:Connect(RightClick)
1 Like

I would start here: Character Pathfinding | Documentation - Roblox Creator Hub. If I see it correctly, it maybe will be more problematic since your world is 2D (or is it 3D?) and - as far as I know - Roblox’s pathfinding is working in 3D. It could be fixed by just placing the map horizontally on the world (if that’s not already the case). But I guess more info on where exactly you’re stuck would be useful, except if you expect someone will now just script this whole thing :sweat_smile:

1 Like

Infact my map is 3D, it’s just the position i took the Screenshot.
I managed to do a simple pathfinding script and it works like a charm.

Script:

--Variables

local PathFindingService = game:GetService("PathfindingService")
local Path = PathFindingService:CreatePath()

local ReplicatedStorage = game.ReplicatedStorage
local RemoteEvent = ReplicatedStorage.RemoteEvents.Units.UnitMove

--Script

--//Move Unit to Province 
local function MoveUnit(Player, TAG, Target, Units)
	print("Start")
	Path:ComputeAsync(Units.UnitModel.PrimaryPart.Position, Target.Position)
	
	local Humanoid = Units.UnitModel:WaitForChild("Humanoid")
	local Waypoints = Path:GetWaypoints()
	
	for i, Waypoints in pairs(Waypoints) do
		Humanoid:MoveTo(Waypoints.Position)
		Humanoid.MoveToFinished:Wait(1)
	end
	
	print("Done?")
end

--//RemoteEvent
RemoteEvent.OnServerEvent:Connect(MoveUnit)

But the question now is that my units move smoothly from one point to another, but i want them to teleport after a certain amount of time, i tried to just do:
PrimaryPart.Position = Waypoint.Position
But it didn’t work. So how can i achieve that?

Here’s a video demonstrating what exactly i want:

1 Like

Maybe for each section, create an invisible part in the middle, then just humanoid:moveto to it

2 Likes

Nevermind, i think i’ll keep it like this, thanks for the help.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.