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

How would you insert both the name of a value and the value itself into a table?

Hi there! I'm trying to mess around with different ways for storing data via DataStore without saving each value individually. Someone I know had suggested trying to save it all with a table, so I'm trying to do that. However, I'm a bit confused as to how I would use table.insert(), or anything else to add the name of the value, and the value itself to the table? Any help is greatly appreciated. :)

What I want:

local Table = {
	["Cash"] = 0
}

Script I've tried using:

local Table = {}
for _,v in pairs(Player["leaderstats"]:GetChildren()) do
	table.insert(Table, {[v.Name] = v.Value})
end

2 comments

1

Just do Table[v.Name] = v.Value

0

In the example you gave, you were inserting a table into Table.

1 answer

✓ Accepted answer

Its simple, dont need to use table.insert you can only use Table[ITEM] = VALUE

This basically creates a new value in a table. because you're basically setting this for the table.

Here is a example:

local Table = {}

Table["Coin"] = 120 -- Add the value to table.
wait()
print(Table["Coin"]) -- Print the value.

Here is your fixed script:

local Table = {} -- Items table

for _,v in pairs(Player["leaderstats"]:GetChildren()) do -- Get all items in leaderstats in player
   Table[v.Name] = v.Value -- Add the values to table.
end

Hope it helped :)

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