Hello! i need help with something specific, i have a unit that i already made a Pathfinding, but my question is how do i find all parts that the Pathfinding goes Through?
Here’s 2 videos to explain better what i want to achieve:
What i want to do:
What i have:
So i want basically to teleport the unit from “part” to “part”, while also allowing the players to select a part that is not adjacent.
-The Script:
--Variables
local PathFindingService = game:GetService("PathfindingService")
local Path = PathFindingService:CreatePath()
local ReplicatedStorage = game.ReplicatedStorage
local RemoteEvent = ReplicatedStorage.RemoteEvents.Client.UnitMove
--Script
--//Move Unit to Province
local function MoveUnit(Player, TAG, Target, Units)
Path:ComputeAsync(Units.UnitModel.PrimaryPart.Position, Target.Position)
local Humanoid = Units.UnitModel:WaitForChild("Humanoid")
local Waypoints = Path:GetWaypoints()
local CurrentProvince = Units.Info.CurrentProvince
for i, Waypoints in pairs(Waypoints) do
Humanoid:MoveTo(Waypoints.Position)
Humanoid.MoveToFinished:Wait(1)
end
end
--//RemoteEvent
RemoteEvent.OnServerEvent:Connect(MoveUnit)
I’m assuming that you want to visualize the waypoints that the pathfinding creates.
You can create a separate function like this:
local function ShowWaypoints(waypoints :{}) --Waypoints are inside a table
for index, point in waypoints do
local part = Instance.new("Part")
part.Name = index
part.Position = point.Position
part.Size = Vector3.one
part.Anchored = true
part.CanCollide = false
part.CanQuery = false
part.CanTouch = false
part.CastShadow = false
part.Parent = workspace
end
end
Then put ShowWaypoints(Waypoints) before the for loop that makes the Humanoid move.
I should have mentioned that i’m changing the system to make the unit teleport after a certain amount of time to the next part that the pathfinding finds… like the video above.
right, then what might be the best option is to pre-emptively send a faster, invisible humanoid to “pre-draw” the pathing and use that humanoid’s position, although i think this will be a little more expensive than other options; doing this on the local should be the least-process-expensive way to do it.
I tried both, when i put .position it says “unable to cast Vector3 to CoordinateFrame”, and when i put CFrame it says " “CFrame” is not a valid member of PathWaypoint".
That worked out pretty well, but another problem now appeared on the horizon.
All the parts have different sizes, for example, while my current parts in this “area” are small, there are going to be “areas” where the parts are smaller/bigger, so how can i overcome this?