Issues with UserInputService KeyCodes

Been tinkering around making a new train system, but it never seems to connect to the value that controls the speed. The output shows nothing at all and nothing seems to work.

local uis = game:GetService("UserInputService")
local seat = script.Parent
local moto = script.Parent.Parent.Motor
local engine = moto.Engine
local val = script.Parent.Notches.Value

uis.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.E then
		print("Notched up")
		val += 1
	end
end)
uis.InputBegan:Connect(function(input2)
	if input2.KeyCode == Enum.KeyCode.Q then
		print("Notched down")
		val -= 1
	end
end)

if val == 0 then
	engine.Force = Vector3.new(0, 0, 0)
end
if val == 1 then
	engine.Force = Vector3.new(774, 0, 0)
end
if val == 2 then
	engine.Force = Vector3.new(776, 0, 0)
end
if val == 3 then
	engine.Force = Vector3.new(778, 0, 0)
end
if val == 4 then
	engine.Force = Vector3.new(800, 0, 0)
end
if val == 5 then
	engine.Force = Vector3.new(802, 0, 0)
end
if val == 6 then
	engine.Force = Vector3.new(804, 0, 0)
end
if val == 7 then
	engine.Force = Vector3.new(806, 0, 0)
end
if val == 8 then
	engine.Force = Vector3.new(808, 0, 0)
end
local uis = game:GetService("UserInputService")
local seat = script.Parent
local moto = script.Parent.Parent.Motor
local engine = moto.Engine
local notches = script.Parent.Notches -- Changed from val to notches

-- Added a function to calculate engine force based on notch value
local function calculateEngineForce(notchValue: number): Vector3
	return Vector3.new(772 + 2 * notchValue, 0, 0)
end

-- Changed: only use one connection for the event
uis.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.E then
		print("Notched up")
		notches.Value += 1  -- Fixed: directly updating notches.Value instead of using a separate val
	elseif input.KeyCode == Enum.KeyCode.Q then
		print("Notched down")
		notches.Value -= 1
	end

	engine.Force = calculateEngineForce(notches.Value)  -- Fixed: engine force now updates every input using a dynamic function
end)

commented the fixed issues and the changed things hope this helps. :slight_smile:

1 Like

The reason that your code isn’t working how you expected isn’t because of the keycodes, but rather because you are only updating the engine force once, on the initial run of the script. To fix this, you would need to move the if statement into the inputbegan / inputended events.

@Terminatorv020’s post does this and also reduces the if statements into a single calculation, as there was a clear pattern between val and the expected force. Do note though that their calculateEngineForce function does not account for a notchValue of 0 correctly, so you may wish to add an if statement to check if notchValue is 0 in which case you would return 0

1 Like

out of curiosity, what type of script are you using for this?

UIS Only gets input on the client and doesn’t work on the server.

You would be better to use UIS to get input then fire a Remote Event to the server on input

Something like this:

local uis = game:GetService("UserInputService")
local Event = path to remote event

uis.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.E then
		print("Input received")
        Event:FireServer(notch up param)
	end
end)
uis.InputBegan:Connect(function(input2)
	if input2.KeyCode == Enum.KeyCode.Q then
		print("Input received")
        Event:FireServer(notch down param)
	end
end)

With this remote event it’s better to use a parameter to tell the server what key was pressed because it lowers clutter and is more optimized than to have two events to be called for one specific thing.

Ah, I should have specified. I am using a localscript for this for that exact reason.