hey there’s I want to achieve a train driverable via player which is controlled via gui but I don’t know how to do it I tried many things but then it isn’t working here is the code which i used:-
Script
local train = script.Parent
local Gui = game.Players.LocalPlayer.PlayerGui.TrainGUI
local SpeedControl = Gui.SpeedFrame
local IncrSpeed = SpeedControl.Incr
local DecSpeed = SpeedControl.Dec
local Speedometer = SpeedControl.Speedometer
local Speed = 0
local Acc = 10
local BrakeBut = Gui.BrakeButton
local Brake = -10
local Light = Gui.LightButton
local LightPart = train.HeadLight.Spotlight
local Horn = Gui.HornButton
local HornSound = train.HornSound1
local CurrentSpeed = 0
local VehicleSeat = script.Parent.Drive
-- Checks if the player is seated in the train's vehicle seat
local function isPlayerSeated()
return VehicleSeat.Occupant == game.Players.LocalPlayer.Character
end
-- Toggles the visibility of the GUI based on whether the player is seated or not
local function toggleGUIVisibility()
Gui.Enabled = isPlayerSeated()
end
Speedometer.Text = "0"
IncrSpeed.MouseButton1Click:Connect(function()
if isPlayerSeated() then
CurrentSpeed = CurrentSpeed + Acc
VehicleSeat.MaxSpeed = CurrentSpeed
Speedometer.Text = tostring(CurrentSpeed)
end
if CurrentSpeed == 90 then
CurrentSpeed = 0
Speedometer.Text = "Damaged"
end
end)
DecSpeed.MouseButton1Click:Connect(function()
if isPlayerSeated() then
CurrentSpeed = CurrentSpeed - Acc
VehicleSeat.MaxSpeed = CurrentSpeed
Speedometer.Text = tostring(CurrentSpeed)
end
if CurrentSpeed == -50 then
CurrentSpeed = 0
Speedometer.Text = "Rev Max"
end
end)
BrakeBut.MouseButton1Click:Connect(function()
if isPlayerSeated() then
while CurrentSpeed > 0 do
CurrentSpeed = CurrentSpeed - Brake
if CurrentSpeed < 0 then
CurrentSpeed = 0
end
VehicleSeat.MaxSpeed = CurrentSpeed
Speedometer.Text = tostring(CurrentSpeed)
wait(0.1) -- Adjust the delay time between speed reductions if needed
end
while CurrentSpeed < 0 do
CurrentSpeed = CurrentSpeed + Acc
if CurrentSpeed > 0 then
CurrentSpeed = 0
end
VehicleSeat.MaxSpeed = CurrentSpeed
Speedometer.Text = tostring(CurrentSpeed)
wait(0.1) -- Adjust the delay time between speed increases if needed
end
end
end)
Light.MouseButton1Click:Connect(function()
if LightPart.Brightness == 0 then
LightPart.Brightness = 6.4
else
LightPart.Brightness = 0
end
end)
Horn.MouseButton1Click:Connect(function()
HornSound:Play()
end)
-- Call the toggleGUIVisibility function to initially show/hide the GUI based on seating status
toggleGUIVisibility()