Hey, i needed a physics-based waypoint following car for my game so i asked ChatGPT to help me (i’m not good at roblox physics stuff), and so far it’s a nightmare. The car just teleports to the first waypoint and does nothing. But the first time, it did it no problem (made it to the last waypoint), and every time after that it just didn’t work.
local car = workspace.Car
local body = car.PrimaryPart
local speed = 50
local waypointIndex = 1
task.wait(5) -- waits for randomly generated road to spawn
-- Sort waypoints numerically
local waypoints = workspace.Waypoints:GetChildren()
table.sort(waypoints, function(a, b)
return tonumber(a.Name:match("%d+")) < tonumber(b.Name:match("%d+"))
end)
-- Move car to first waypoint
local spawnCFrame = waypoints[1].CFrame + Vector3.new(0, 3, 0)
car:SetPrimaryPartCFrame(spawnCFrame)
car:PivotTo(spawnCFrame)
-- Reset velocities
for _, part in ipairs(car:GetDescendants()) do
if part:IsA("BasePart") then
part.AssemblyLinearVelocity = Vector3.zero
part.AssemblyAngularVelocity = Vector3.zero
end
end
-- Check if car is anchored
if body.Anchored then
print("Car is anchored. Unanchor it to allow movement.")
return
end
-- Create persistent BodyVelocity and BodyGyro
local velocity = Instance.new("BodyVelocity")
velocity.MaxForce = Vector3.new(1e5, 0, 1e5)
velocity.P = 1e4
velocity.Velocity = Vector3.zero
velocity.Parent = body
local gyro = Instance.new("BodyGyro")
gyro.MaxTorque = Vector3.new(0, 1e6, 0)
gyro.P = 1e4
gyro.D = 100
gyro.CFrame = body.CFrame
gyro.Parent = body
game:GetService("RunService").Stepped:Connect(function(_, dt)
local target = waypoints[waypointIndex]
if not target then
velocity.Velocity = Vector3.zero -- Stop the car
return
end
local dir = (target.Position - body.Position)
local distance = dir.Magnitude
if distance < 5 then
waypointIndex += 1
-- If no more waypoints, stop the car
if waypointIndex > #waypoints then
velocity.Velocity = Vector3.zero
end
return
end
dir = dir.Unit
velocity.Velocity = dir * speed
gyro.CFrame = CFrame.new(body.Position, body.Position + dir)
end)