Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Why does this array have 0 members?

Why does this print 0? I really don't understand why.

local kRef = {
	["w"] = Enum.KeyCode.W,
	["a"] = Enum.KeyCode.A,
	["s"] = Enum.KeyCode.S,
	["d"] = Enum.KeyCode.D,
	["space"] = Enum.KeyCode.Space
}
print(#kRef)

2 answers

✓ Accepted answer

###When a dictionary is created, the keys inside of it do not have an order. Because of that, iterating through a dictionary may not give the values in the order you wrote. This is also why #keyRef printed 0, because of how the keys are stored. In order to fix this, you should remove the dictionary's indexes and make it a normal table.

local kRef = {
    Enum.KeyCode.W,
    Enum.KeyCode.A,
    Enum.KeyCode.S,
    Enum.KeyCode.D,
    Enum.KeyCode.Space
}
print(#kRef) --> 5

local kRef = {
    ["w"] = Enum.KeyCode.W,
    ["a"] = Enum.KeyCode.A,
    ["s"] = Enum.KeyCode.S,
    ["d"] = Enum.KeyCode.D,
    ["space"] = Enum.KeyCode.Space
}

--If you want to print all the table's contents, use a loop:
for i, v in pairs(kRef) do 
	print(v)
end

1 comment

0

I don't want the content, I want the size, and the size is 0.

This discussion is preserved as it was. New answers, comments, and votes are closed.