Why is this returning nil?

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.

wut

Code? I need more information to know why is it doing this?

1 Like

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)

Hmmmm are you setting the values name?

1 Like

That’s because when you’re making a table without explicitly stating an index, the items get listed from 1 to #arguments in order, so really,

{Enum.KeyCode.One, Enum.KeyCode.Two, Enum.KeyCode.Three, Enum.KeyCode.Four, Enum.KeyCode.Five}

is equal to

{
    [1] = Enum.KeyCode.One, 
    [2] = Enum.KeyCode.Two, 
    [3] = Enum.KeyCode.Three, 
    [4] = Enum.KeyCode.Four, 
    [5] = Enum.KeyCode.Five
}

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.

1 Like

No, there is nothing that changes val.Name, only things that change val. And val can never exceed 5, that I’ve made sure of.

I’ve tried that. It doesn’t work.

Hmm, what is val.Name equal to?

1 Like

I am pretty sure that setting it as numbers well not work. Instead try this.

{
    ["1"] = Enum.KeyCode.One, 
    ["2"] = Enum.KeyCode.Two, 
    ["3"] = Enum.KeyCode.Three, 
    ["4"] = Enum.KeyCode.Four, 
    ["5"] = Enum.KeyCode.Five
}
1 Like

He wasn’t telling him to manually specify the numeric indexes, he was just showing him that his original table was the equivalent of that.

2 Likes

1, 2, 3, 4, or 5. val is always an Instance within this:
1

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?

1 Like

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)

the third value of print should say 1

3 Likes

Oh ok.
Use this code:

--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)
1 Like

Well, that would allow any button press to be used instead of only one, which isn’t how the game is meant to work.

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

Any button on the keyboard or the buttons?

This is bizarre, I ran this code in the command bar in Studio and it worked just fine.

local a = {"A", "B", "C"}

print(a[tonumber("1")]) -- >prints A

Show us your full script.

Any button 1-5 instead of just, say 2.

So you only want one button to be pressed?

1 Like