I have this code, it is intended to make a car drive along a set of checkpoints, however the car does not drive to the checkpoint and does it’s own thing. You can see the video here
local checkpoints = game.Workspace.CarCheckpoints
local Force = 5000
local MaxSteer = 15
local MaxSpeedKM = 50
local car = script.Parent.Parent
local seat = script.Parent
local leftMotor = car.AttachBase.Left
local rightMotor = car.AttachBase.Right
local targetCheckpoint = nil :: BasePart
local speedSet
function goToCheckpoint(checkpointName)
print(checkpointName)
targetCheckpoint = checkpoints:FindFirstChild(checkpointName)
speedSet = seat.ThrottleFloat * Force
targetCheckpoint.Touched:Connect(function(hit)
if hit and hit.Parent == car then
if checkpoints:FindFirstChild(checkpointName + 1) then
goToCheckpoint(checkpointName + 1)
else
targetCheckpoint = nil
end
end
end)
end
goToCheckpoint("1")
while task.wait(0.1) do
if targetCheckpoint ~= nil then
local checkpointPosition = targetCheckpoint.Position
local seatPosition = car.AttachBase.Position
local hyp = math.sqrt((seatPosition.X - checkpointPosition.X)^2 + (seatPosition.Z - checkpointPosition.Z)^2)
local sad = seatPosition.X - checkpointPosition.X
local angle = math.deg(math.asin(sad/hyp))
print(angle)
leftMotor.TargetAngle = angle
rightMotor.TargetAngle = angle
car.BackleftWheel.LeftDrive.AngularVelocity= -30
car.BackRightWheel.RightDrive.AngularVelocity = -30
end
end