I’m trying to make a function that binds keyboard input from every key (except WASD, I and O since those are used for moving and camera) using a for loop. Every time I run this, only the last key (Z) is bound. The other keys are somehow being unbound? I know it’s possible to bind multiple keys to the same function, so what is causing the other keys to unbind?
Also, there has to be a better way of doing this than listing every single key code in a table right?
Anyone know what I’m missing here? (yes I am working in a local script)
local cas = game:GetService("ContextActionService")
local letters = {
Enum.KeyCode.B,
Enum.KeyCode.C,
Enum.KeyCode.E,
Enum.KeyCode.F,
Enum.KeyCode.G,
Enum.KeyCode.H,
Enum.KeyCode.J,
Enum.KeyCode.K,
Enum.KeyCode.L,
Enum.KeyCode.M,
Enum.KeyCode.N,
Enum.KeyCode.P,
Enum.KeyCode.Q,
Enum.KeyCode.R,
Enum.KeyCode.T,
Enum.KeyCode.U,
Enum.KeyCode.V,
Enum.KeyCode.X,
Enum.KeyCode.Y,
Enum.KeyCode.Z,
}
local numbers = {
Enum.KeyCode.One,
Enum.KeyCode.Two,
Enum.KeyCode.Three,
Enum.KeyCode.Four,
Enum.KeyCode.Five,
Enum.KeyCode.Six,
Enum.KeyCode.Seven,
Enum.KeyCode.Eight,
Enum.KeyCode.Nine,
Enum.KeyCode.Zero,
}
local function OnInput(actionName, inputState)
if inputState == Enum.UserInputState.Begin then
if actionName == "letter" then
print ("letter pressed")
elseif actionName == "number" then
print ("number pressed")
end
end
end
for index, item in pairs(letters) do
cas:BindAction("letter", OnInput, false, item)
end
for index, item in pairs(numbers) do
cas:BindAction("number", OnInput, false, item)
end