Train Notch Throttle Script

Hello. I am currently making a train simulator but I need some help with making a notch throttle script. I want to make it so when the player uses the W and S keys, the throttle will move up or down to the power or braking. I tried to make a script before but it was extremely long and did not work, but I do know I need to start with something like this:

if inputObject.KeyCode == Enum.KeyCode.W then

Another thing I think I will need is a value that holds the current throttle number, and I can make a ScreenGui to have everything on. The train’s driving system will be done with BodyVelocities, no animations.

Can anyone help me with how to put this together? Thanks.

1 Like

A basic script for controlling the throttle in a train simulator could look like this:

local throttlePosition = 0 – Current position of the throttle
local maxThrottle = 10 – Maximum throttle position
local minThrottle = -10 – Minimum throttle position

– Functions for changing the throttle position
local function increaseThrottle()
if throttlePosition < maxThrottle then
throttlePosition = throttlePosition + 1
print("Throttle increased to: " … throttlePosition)
end
end

local function decreaseThrottle()
if throttlePosition > minThrottle then
throttlePosition = throttlePosition - 1
print("Throttle decreased to: " … throttlePosition)
end
end

– Key assignment
local UserInputService = game:GetService(“UserInputService”)

UserInputService.InputBegan:Connect(function(input)
if input.KeyCode == Enum.KeyCode.W then
increaseThrottle()
elseif input.KeyCode == Enum.KeyCode.S then
decreaseThrottle()
end
end)

– Example of applying the throttle to the train’s propulsion system
local function applyThrottle()
– This is where your code would go to change the speed of the train
– Example:
local train = workspace.Train – Replace “Train” with the name of your train
local bodyVelocity = train:FindFirstChild(“BodyVelocity”)

if bodyVelocity then
bodyVelocity.Velocity = Vector3.new(throttlePosition, 0, 0) – Adjusting depending on the axis
end
end

– Constantly updating the throttle position to the train’s propulsion system
game:GetService(“RunService”).RenderStepped:Connect(function()
applyThrottle()
end)

( I am not the best scripter, i have my best, please don’t be sad If It Not work )

1 Like

Thank you very much, this is amazing! I should be able to do a maximum speed limit and an emergency brake myself, and whatever else I need.

1 Like

You welcome, how will you Name the Game ?:grin:

By the way can you maybe help me too? BecoasenI don’t know anything about Game passes :disappointed_relieved:

It will be called Robloxian Railways, here’s the group link if you are interested! Robloxian Railways Official Group - Roblox

The game is currently private as it is in its main development phase, but we do have development updates in the community server.

I know how to make a gamepass button or something like that, I can show you how

Do you know how to make A piece that adds specific clothing and accessories to a Roblox character by clicking an image?

You would need to have a ScreenGui (if you mean an image like on the screen) with an ImageButton in it. I tried making a script here to change the clothing IDs of the player who presses the button, I hope this works!


local shirtID = 0
local pantsID = 0

script.Parent.MouseButton1Down:connect(function(player)
	if player ~= nil then
		player:FindFirstChild("Shirt").ShirtTemplate = "http://www.roblox.com/asset/?id=" .. shirtID
		player:FindFirstChild("Pants").PantsTemplate = "http://www.roblox.com/asset/?id=" .. pantsID
	end
end)```

Thankyou, i will try it later :slight_smile:

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.