Keyless
Key Required
Auto Race Checkpoints
Lua Script•134 lines (5050 chars)
local Players = game:GetService("Players")
local UserInputService = game:GetService("UserInputService")
local RunService = game:GetService("RunService")
local Player = Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")
local toggleKey = Enum.KeyCode.P -- Key to start/stop
local moveSpeed = 350 -- Constant car speed
local tolerance = 5 -- Distance threshold to consider checkpoint reached
local autoDriveEnabled = false
local currentVehicle = nil
local checkpoints = {}
local finishLine = nil
local hasReachedFinishLine = false
local function GetVehicleFromDescendant(part)
while part and part ~= game do
if part:IsA("Model") then
return part
end
part = part.Parent
end
return nil
end
local function FindAllRaceCheckpoints()
local racesFolder = workspace:FindFirstChild("Races")
if not racesFolder then
return {}
end
local checkpointVisuals = {}
for _, child in pairs(racesFolder:GetDescendants()) do
if child.Name == "RaceCheckpointVisual" and child:IsA("BasePart") then
table.insert(checkpointVisuals, child)
elseif child.Name == "LastRaceCheckpointVisual" and child:IsA("BasePart") then
finishLine = child
end
end
table.sort(checkpointVisuals, function(a, b)
return a.Name < b.Name
end)
return checkpointVisuals
end
local function StopCar(vehicle)
if vehicle and vehicle.PrimaryPart then
vehicle.PrimaryPart.AssemblyLinearVelocity = Vector3.zero
end
end
local function DriveCarToLocation(vehicle, targetLocation)
local primaryPart = vehicle.PrimaryPart
if not primaryPart then
return
end
local direction = (targetLocation - primaryPart.Position).Unit
primaryPart.AssemblyLinearVelocity = direction * moveSpeed
primaryPart.CFrame = CFrame.new(primaryPart.Position, primaryPart.Position + direction)
end
local function ToggleAutoDrive()
autoDriveEnabled = not autoDriveEnabled
if autoDriveEnabled then
local SeatPart = Humanoid.SeatPart
if SeatPart and SeatPart:IsA("VehicleSeat") then
currentVehicle = GetVehicleFromDescendant(SeatPart)
if currentVehicle and currentVehicle:IsA("Model") then
checkpoints = FindAllRaceCheckpoints()
hasReachedFinishLine = false
end
end
else
currentVehicle = nil
end
end
coroutine.wrap(function()
while true do
if autoDriveEnabled then
if currentVehicle and currentVehicle.PrimaryPart then
if #checkpoints > 0 then
local currentCheckpoint = checkpoints[1]
if currentCheckpoint and currentCheckpoint.Parent then
local targetLocation = currentCheckpoint.Position
local distanceToTarget = (currentVehicle.PrimaryPart.Position - targetLocation).Magnitude
DriveCarToLocation(currentVehicle, targetLocation)
if distanceToTarget <= tolerance then
table.remove(checkpoints, 1)
local nextCheckpointFound = false
while not nextCheckpointFound do
wait(0.2)
checkpoints = FindAllRaceCheckpoints()
if #checkpoints > 0 then
nextCheckpointFound = true
else
StopCar(currentVehicle)
end
end
end
else
table.remove(checkpoints, 1)
end
elseif finishLine and not hasReachedFinishLine then
local targetLocation = finishLine.Position
local distanceToFinish = (currentVehicle.PrimaryPart.Position - targetLocation).Magnitude
DriveCarToLocation(currentVehicle, targetLocation)
if distanceToFinish <= tolerance then
hasReachedFinishLine = true
finishLine = nil -- Clear the finish line to prevent looping back
ToggleAutoDrive()
print("Reached the finish line! Looking for new checkpoints...")
end
else
StopCar(currentVehicle)
wait(0.2)
checkpoints = FindAllRaceCheckpoints()
end
else
wait(0.2)
checkpoints = FindAllRaceCheckpoints()
end
end
RunService.Heartbeat:Wait()
end
end)()
UserInputService.InputBegan:Connect(function(input, gameProcessed)
if gameProcessed then return end
if input.KeyCode == toggleKey then
ToggleAutoDrive()
end
end)Features & Information
when in line to race execute and "P" is to enable and disable.
will need to start race manually every time im to slow to make this work
welcome to take it and make it better.
thanks Deep Seek for fixing, finish checkpoint and then reorganized the script -_- but still works so meh
Community Comments (0)
No comments yet for this script.
