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

How to make each key equal to its value in an array?

So I want to try to make each key equal to its value instead of the keys being 1, 2, 3, etc.

local currentplates = plateholder:GetChildren()

That's what I used to get an array with plateholder's children.

I've looked through many resources and haven't been able to find out how.

1 answer

✓ Accepted answer

local aTable = {}
local aString = "JustAString"

aTable[aString] = true

The key is "JustAstring" and the value is true. After you set the value, these two are exactly the same way to point to the position in the table:

local aTable = {}
local aString = "JustAString"

aTable[aString] = true
print(aTable[aString])
--prints true
print(aTable.JustAString)
--prints true

It's important to know you can only do this with string keys, so use an objects.Name if you wish to store object references in the table.

And to show that it is indeed adding different records:

local aTable = {}
local aString = "JustAString"
aTable[aString] = true
aString = "ADifferentString"
aTable[aString] = 25
print(aTable.JustAString)
print(aTable[aString])
--prints: true, then 25
--The Table now has two records in it, the first with a value of true, and the second with a value of 25

2 comments

0

Thanks, this really helps.

0

Any time, glad to help.

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