Throttle control with keybind not working

Hello,
I am currently making a train game and i want to make a throttle control system by using W to increase the speed and S to decrease the speed.
It is not working and i cant figure it out. I already looked into others topics but i can’t find the solution.

local backleft1 = script.Parent.Parent.Train.Lokomotiv.BackBogie.BackLeft.WheelDown.WheelConstraint
local backleft12 = script.Parent.Parent.Train.Lokomotiv.BackBogie.BackLeft.WheelUp.WheelConstraint
local backright2 = script.Parent.Parent.Train.Lokomotiv.BackBogie.BackRight.WheelDown.WheelConstraint
local backright22 = script.Parent.Parent.Train.Lokomotiv.BackBogie.BackRight.WheelUp.WheelConstraint
local frontleft1 = script.Parent.Parent.Train.Lokomotiv.BackBogie.BackLeft.WheelDown.WheelConstraint
local frontleft12 = script.Parent.Parent.Train.Lokomotiv.BackBogie.BackLeft.WheelUp.WheelConstraint
local frontright2 = script.Parent.Parent.Train.Lokomotiv.BackBogie.BackRight.WheelDown.WheelConstraint
local frontright22 = script.Parent.Parent.Train.Lokomotiv.BackBogie.BackRight.WheelUp.WheelConstraint

local backleft3 = script.Parent.Parent.Train.Lokomotiv.FrontBogie.BackLeft.WheelDown.WheelConstraint
local backleft34 = script.Parent.Parent.Train.Lokomotiv.FrontBogie.BackLeft.WheelUp.WheelConstraint
local backright4 = script.Parent.Parent.Train.Lokomotiv.FrontBogie.BackRight.WheelDown.WheelConstraint
local backright44 = script.Parent.Parent.Train.Lokomotiv.FrontBogie.BackRight.WheelUp.WheelConstraint
local frontleft5 = script.Parent.Parent.Train.Lokomotiv.FrontBogie.BackLeft.WheelDown.WheelConstraint
local frontleft56 = script.Parent.Parent.Train.Lokomotiv.FrontBogie.BackLeft.WheelUp.WheelConstraint
local frontright78 = script.Parent.Parent.Train.Lokomotiv.FrontBogie.BackRight.WheelDown.WheelConstraint
local frontright98 = script.Parent.Parent.Train.Lokomotiv.BackBogie.BackRight.WheelUp.WheelConstraint


local vehicle = script.Parent.Parent.Train.Lokomotiv.VehicleSeat
local speed = vehicle.Throttle
local UserInputService = game:GetService("UserInputService")

local function increaseSpeed()
	speed = speed + 1
	vehicle:SetPrimaryPartCFrame(vehicle:GetPrimaryPartCFrame() + vehicle:GetPrimaryPart().CFrame.lookVector * speed)
	
	backleft1.AngularVelocity = Vector3.zAxis(speed + 1 )
	backleft12.AngularVelocity = Vector3.zAxis(speed + 1 )
	backright2.AngularVelocity = Vector3.zAxis(speed + 1 )
	backright22.AngularVelocity = Vector3.zAxis(speed + 1 )
	frontleft1.AngularVelocity = Vector3.zAxis(speed + 1 )
	frontleft12.AngularVelocity = Vector3.zAxis(speed + 1 )
	frontright2.AngularVelocity = Vector3.zAxis(speed + 1 )
	frontright22.AngularVelocity = Vector3.zAxis(speed + 1 )

	backleft3.AngularVelocity = Vector3.zAxis(speed + 1 )
	backleft34.AngularVelocity = Vector3.zAxis(speed + 1 )
	backright4.AngularVelocity = Vector3.zAxis(speed + 1 )
	backright44.AngularVelocity = Vector3.zAxis(speed + 1 )
	frontleft5.AngularVelocity = Vector3.zAxis(speed + 1 )
	frontleft56.AngularVelocity = Vector3.zAxis(speed + 1 )
	frontright78.AngularVelocity = Vector3.zAxis(speed + 1 )
	frontright98.AngularVelocity = Vector3.zAxis(speed + 1 )
end

local function decreaseSpeed()
	speed = speed - 1
	if speed <= 0 then
		speed = 0
	end
	vehicle:SetPrimaryPartCFrame(vehicle:GetPrimaryPartCFrame() + vehicle:GetPrimaryPart().CFrame.lookVector * speed)
	
	backleft1.AngularVelocity = Vector3.zAxis(speed - 1 )
	backleft12.AngularVelocity = Vector3.zAxis(speed - 1 )
	backright2.AngularVelocity = Vector3.zAxis(speed - 1 )
	backright22.AngularVelocity = Vector3.zAxis(speed - 1 )
	frontleft1.AngularVelocity = Vector3.zAxis(speed - 1 )
	frontleft12.AngularVelocity = Vector3.zAxis(speed - 1 )
	frontright2.AngularVelocity = Vector3.zAxis(speed - 1 )
	frontright22.AngularVelocity = Vector3.zAxis(speed - 1 )

	backleft3.AngularVelocity = Vector3.zAxis(speed - 1 )
	backleft34.AngularVelocity = Vector3.zAxis(speed - 1 )
	backright4.AngularVelocity = Vector3.zAxis(speed - 1 )
	backright44.AngularVelocity = Vector3.zAxis(speed - 1 )
	frontleft5.AngularVelocity = Vector3.zAxis(speed - 1 )
	frontleft56.AngularVelocity = Vector3.zAxis(speed - 1 )
	frontright78.AngularVelocity = Vector3.zAxis(speed - 1 )
	frontright98.AngularVelocity = Vector3.zAxis(speed - 1 )
end

UserInputService.InputBegan:Connect(function(input, gameProcessedEvent)
	if not gameProcessedEvent then
		if input.KeyCode == Enum.KeyCode.W then
			increaseSpeed()
		elseif input.KeyCode == Enum.KeyCode.S then
			decreaseSpeed()
		end
	end
end)

When i run the game and enter the vehicle, by pressing W or S it does nothing. And the console doesn’t show anything too.

What is wrong with my script ?

I am available for more details.

Thanks for reading.

I think you can only get the inputs from a player from the client side, so use a remote event. Also this script is poorly made. You use “script.Parent.Parent.Train.Lokomotiv” a lot of times, just do a thing called Lokomotiv and and you’re going to make it so much more readable, also make a table containing all the part that you change the angular velocity so you can use a for loop to make them all do the same thing without rewriting the same line over and over.

I hope you fixed it in the two days it took me to respond.

I’m bored, I reworked your script

Server script:

local Lokomotiv = script.Parent.Parent.Train.Lokomotiv

local Wheels = {
backleft1 = Lokomotiv.BackBogie.BackLeft.WheelDown.WheelConstraint,
backleft12 = Lokomotiv.BackBogie.BackLeft.WheelUp.WheelConstraint,
backright2 = Lokomotiv.BackBogie.BackRight.WheelDown.WheelConstraint,
backright22 = Lokomotiv.BackBogie.BackRight.WheelUp.WheelConstraint,
frontleft1 = Lokomotiv.BackBogie.BackLeft.WheelDown.WheelConstraint,
frontleft12 = Lokomotiv.BackBogie.BackLeft.WheelUp.WheelConstraint,
frontright2 = Lokomotiv.BackBogie.BackRight.WheelDown.WheelConstraint,
frontright22 = Lokomotiv.BackBogie.BackRight.WheelUp.WheelConstraint,

backleft3 = Lokomotiv.FrontBogie.BackLeft.WheelDown.WheelConstraint,
backleft34 = Lokomotiv.FrontBogie.BackLeft.WheelUp.WheelConstraint,
backright4 = Lokomotiv.FrontBogie.BackRight.WheelDown.WheelConstraint,
backright44 = Lokomotiv.FrontBogie.BackRight.WheelUp.WheelConstraint,
frontleft5 = Lokomotiv.FrontBogie.BackLeft.WheelDown.WheelConstraint,
frontleft56 = Lokomotiv.FrontBogie.BackLeft.WheelUp.WheelConstraint,
frontright78 = Lokomotiv.FrontBogie.BackRight.WheelDown.WheelConstraint,
frontright98 = Lokomotiv.BackBogie.BackRight.WheelUp.WheelConstraint
}

local vehicle = Lokomotiv.VehicleSeat
local speed = vehicle.Throttle

local function increaseSpeed()
speed += 1
vehicle:SetPrimaryPartCFrame(vehicle:GetPrimaryPartCFrame() + vehicle:GetPrimaryPart().CFrame.lookVector * speed)

for _, wheel in ipairs(Wheels) do
wheel.AngularVelocity = Vector3.zAxis(speed + 1)
end
end

local function decreaseSpeed()
speed -= 1
if speed <= 0 then
speed = 0
end
vehicle:SetPrimaryPartCFrame(vehicle:GetPrimaryPartCFrame() + vehicle:GetPrimaryPart().CFrame.lookVector * speed)

for _, wheel in ipairs(Wheels) do
wheel.AngularVelocity = Vector3.zAxis(speed - 1)
end
end

game.ReplicatedStorage.YourEvent.OnServerEvent:Connect(function(player, input)
if input == Enum.KeyCode.W then
increaseSpeed()
else
decreaseSpeed()
end
end)

Client script:

local event = game:GetService("ReplicatedStorage").YourEvent

game:GetService("UserInputService").InputBegan:Connect(function(input, gameProcessedEvent)
if not gameProcessedEvent then
if input.KeyCode == Enum.KeyCode.W then
event:FireServer(input)
elseif input.KeyCode == Enum.KeyCode.S then
event:FireServer(input)
end
end
end)

This may have errors

1 Like

Hey !

First, thanks for responding to me. And, i tested it in game and received this error:

GetPrimaryPartCFrame is not a valid member of VehicleSeat "Workspace.Train.Train.TrainModel.Lokomotiv.VehicleSeat"

Sorry for the late response, but anyways, I searched up :GetPrimaryPartCFrame() in the docs and it seems to be deprecated. So don’t use it, use

model.PrimaryPart.CFrame

also it has to be a model so maybe the error comes from the fact that VehicleSeat isn’t a model which could be logic since train doesn’t have seats (so no model is required) and there’s a thing you can add which is literally called “VehicleSeat”. Hope this helps again.