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

Is it possible to have 2 values in a table have the same key?

So, is it possible to have 2 values in a table have the same key? Heres an example:

local scores = {}
scores [3] = "lucas4114"
scores [3] = "other guy"
scores [2] = "player"
scores [1] = "noob"
print(table.concat(scores, ", "))

it would just print {"noob", "player", "other guy"} and replace "lucas4114" with "other guy". How can I make it so all names get printed in the right order even if 2 players had the same score?

1 answer

✓ Accepted answer

Short answer: no

Tables work in a way in which they're given a specific key, to retrieve that adjacent value. It wouldn't make much sense to have two of the same keys in a table, since the computer wouldn't know which one you're trying to get, so it just returns the one last updated.


The process of setting a value to a key that already exists, is called overwriting. Once you change that key, it will now hold that single value you updated it with - it will not save it's previous values.

In the code you provided, you're using an array (a table with numeric indices). However, the same logic applies for dictionary written tables as well. Saying something like ...

local Dictionary = {
	x = "a",
	y = "b",
	y = "c",
}

print(Dictionary.y) -- prints "c"

.. will print "c", since that's the last value it was updated with. Hope this helped.

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