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.
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 )
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)```