Hello friends,
I am trying to make a vehicle automatically change speed and the code I wrote isn’t working. The train works fine when a player drives it and the numbers are printing in the output, but it wont move automatically.
local vehicle = script.Parent
local seat = vehicle:WaitForChild("VehicleSeat")
local touchPart = vehicle.touchpart
local RunService = game:GetService("RunService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local startEvent = ReplicatedStorage:WaitForChild("StartBusMovement")
local canMove = true
local throttle = 0
local steer = 0
local active = true
local TURN_STEER = 0.8
local LANE_CHANGE_STEER = 0.35
local triggered = {}
local function startAction()
throttle = 0.4
steer = 0
print("Start")
end
local function number1()
throttle = 0.4
steer = 0
print("1")
end
local function number2()
throttle = 0.45
steer = 0
print("2")
end
local function number3()
throttle = 0.5
steer = 0
print("3")
end
local function number4()
throttle = 0.55
steer = 0
print("4")
end
local function number5()
throttle = 0.6
steer = 0
print("5")
end
local function number6()
throttle = 0.65
steer = TURN_STEER
print("6")
end
local function number7()
throttle = 0.7
steer = 0
print("7")
end
local function number8()
throttle = 0.75
steer = 0
print("8")
end
local function number9()
throttle = 0.8
steer = 0
print("9")
end
local function number10()
throttle = 0.9
steer = 0
print("10")
end
local function number11()
throttle = 1
steer = 0
print("11")
end
local numberActions = {
["Start"] = startAction,
[1] = number1,
[2] = number2,
[3] = number3,
[4] = number4,
[5] = number5,
[6] = number6,
[7] = number7,
[8] = number8,
[9] = number9,
[10] = number10,
[11] = number11
}
RunService.Heartbeat:Connect(function()
if not active or not canMove then
seat.ThrottleFloat = 0
seat.SteerFloat = 0
return
end
if math.abs(steer) < 0.01 then
seat.SteerFloat = 0
else
seat.SteerFloat = steer
end
seat.ThrottleFloat = throttle
end)
touchPart.Touched:Connect(function(hit)
if not hit:IsA("BasePart") then return end
local folder = workspace:FindFirstChild("Bus Pathfinding")
if not folder or not hit:IsDescendantOf(folder) then return end
if triggered[hit] then return end
triggered[hit] = true
local partName = hit.Name
if numberActions[partName] then
numberActions[partName]()
else
local partNum = tonumber(partName)
if partNum and numberActions[partNum] then
numberActions[partNum]()
end
end
end)
startEvent.OnServerEvent:Connect(function()
print("StartBusMovement fired")
canMove = true
end)
Any help is greatly appreciated and I’m able to give more details as needed.