So, I’ve been working on a game that requires user input, as most games do. However, I’ve run into a strange problem with it. To start, the input required depends on a certain value, called val.Name, val being a BoolValue Instance with a name of 1, 2, 3, 4, or 5. keyCodeTable is a list/table/whatever that looks like this: {Enum.KeyCode.One, Enum.KeyCode.Two, Enum.KeyCode.Three, Enum.KeyCode.Four, Enum.KeyCode.Five}. Now, here’s the problem.
keyCodeTable[val.Name] is nil. keyCodeTable[tonumber(val.Name)] is nil. keyCodeTable[1] and all other ascending values to 5 are not nil, they are the correct places in the list/table/whatever. val.Name is not nil, and is the correct number.
There is literally nothing else that affects these values, except for val.Name, but as I said earlier, I’ve printed it both before and after the if statement (shown below), and it’s only the number. In fact, keyCodeTable is used only for this. I literally do not even have a mention of it anywhere other than where it’s defined, and the main function, which I guess I’ll just put here.
--Input
local inputFunc
inputFunc = game:GetService("UserInputService").InputBegan:Connect(function(input)
if input.KeyCode == keyCodeTable[val.Name] and inputAllowed then contin += 1 end
inputFunc:Disconnect()
end)
Since val.Name (assuming val is an instance) is a string, it can never be equal to an index corresponding to an entry in the table. You have to use tonumber on val's name.
Okay, when you say keyCodeTable[tonumber(val.Name)] is nil, do you mean it’s giving an error that says attempt to index table with nil, or does it just return nil without an error? Are you sure you’re indexing the right table?
Let’s see if it really is a number or if there is an empty space
local inputFunc
inputFunc = game:GetService("UserInputService").InputBegan:Connect(function(input)
local N = tonumber(val.name)
print(N, val.Name, #val.Name)
if N and input.KeyCode == keyCodeTable[N] and inputAllowed then contin += 1 end
inputFunc:Disconnect()
end)
--Input
local inputFunc
local keyCodeInput
inputFunc = game:GetService("UserInputService").InputBegan:Connect(function(input)
for _,v in pairs(keyCodeTable) do
if v == input.KeyCode then
keyCodeInput = v
end
end
inputFunc:Disconnect()
end)
Well, I’ve printed all of the values that are present, including val.Name (not nil), each position in keyCodeTable (not nil) both before and after the if statement, still within the function. val.Name is always a number, the positions in keyCodeTable are always present.
also power might go out so sorry if i dont respond soon just gonna send this early just in case then edit