Here’s a sample code that you can use to create a pathfinding script for a part in Roblox:
local part = game.Workspace.Part -- Replace "Part" with the name of your desired part
local pathfindingService = game:GetService("PathfindingService")
local path = pathfindingService:CreatePath()
local destination = Vector3.new(0, 5, 0) -- Replace these coordinates with the destination coordinates
path:ComputeAsync(part.Position, destination)
local waypoints = path:GetWaypoints()
for _, waypoint in ipairs(waypoints) do
local move = Instance.new("MoveTo")
move.Target = waypoint.Position
move.Parent = part
move:Play()
move.Completed:Wait()
end
Simply replace the name of the part you want to create a pathfinding script for, as well as the destination coordinates, and this code should work for you. Have fun!
I can’t stop laughing. It’s obvious this was written using ChatGPT.
Anyways, the code above will mostly work, but you need to replace these lines:
With this:
local speed = 5 --replace with speed you want the part to go at.
local t = 0
local max = (part.Position-waypoint.Position).Magnitude/speed
local waitTime = 0 --increase if laggy
while t < max do
part.CFrame = part.CFrame:Lerp(CFrame.lookAt(part.CFrame.Position, waypoint.Position), t/max)
t += task.wait(waitTime)
end
Code Written on DevForum. It might not work and have formatting errors.