Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 6 years ago

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
1
Just do Table[v.Name] = v.Value User#25115 0 — 6y
0
In the example you gave, you were inserting a table into Table. User#25115 0 — 6y

1 answer

Log in to vote
0
Answered by
yHasteeD 1819 Moderation Voter
6 years ago
Edited 6 years ago

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 :)

Ad

Answer this question