Having a name and value in a table

Hi there,

I’ve been doing some research and cannot find the answer to my question. I’m trying to make a table have several names and values to each name.

Example:
TABLE 1
TITLE -> VALUE
TITLE2 -> VALUE

Thanks for your help!

2 Likes

Do you mean a table dictionary?

Example:
Table = {Name1=Value1,Name2=Value2}

print(Table.Name1)

Output:
Value1

local table1 = {
    ["Name with a space"] = 199,
    NormalName = 200
}

You can create tables in Lua by following the above format. You can use Russell’s method of accessing or you can use brackets with the name is oddly formatted like ["Name with a space"]. If you’re accessing elements, you can do table1["Name with a space"]. You say that you want to add multiple values to a single name, you can do do so by assigning an array table to the listed key in the table. So if you wanted to add a new name with a table value to your table you could do something like this:

table1["new name"] = { Coins = 100, Gems = 2 }

Dictionaries aren’t a very complex thing to learn. In fact, they’re one of the more easier things to utilize and can be extremely useful in a lot of cases.

local arr = {["Key"] = "Value"}
local arr = {Key = "Value"} --Identical to what was done above.

You can index the two provided dictionaries above either by. ‘arr[“Key”]’ or ‘arr.Key’. Both will return the values defined. Inserting a new key into a dictionary isn’t very hard to do either. You just need to index a new key. And when it comes to removing, all you need to do is set the key’s value to nil.

local arr = {}
arr["NewKey"] = "NewValue"
--Alternatively: Arr.NewKey = "NewValue
6 Likes

The issue is I would like to add a value into the table apart from defining it at first.

Does table insert have a use?

Oh thank you! That solves it! I thought this wasn’t possible.

One more question, how would I have a loop go through ever value in a table?

You could just redefine each value inside the script.

(By your variables you could do)

while true do
arr[“NewKey”] = “NewValue”
end

If you knew the all the names in the script.

If you do not know the titles of the values in the script, this solution would not work.

That is not what I need.

I need to loop through each key.

Thanks though.

All you’d need to do for this is use a iterating for loop.

for key, value in pairs (array) do
	print(key, value)
end

These loops iterate through each key within the dictionary or array provided. Which you need to use these loops when iterating through dictionaries, because a numerical loop will not work with dictionaries.
Iterating loops can also be used to loop through a generic table. Which key is the position within the table, and the value remains the value stored at the position.

2 Likes

Final question, I promise.

for key, value in pairs (playertable) do
	local name = tostring(key)
	print(data.name)
end

I’m trying to get a JSON object value but I cannot seem to use :FindFirstChild() to get the object value by its name. Is there a workaround?

If your wondering what this is for, I’m sending a HTTPService get request every second to retrieve ever player’s ban status within my web server (objective: if a web server says a player is still banned, remove from game).

Are you trying to get a readable JSON value from a web server?

If yes, you must encode it via HTTPService.

That’s already been done, so yes. :slight_smile:

Hey again,

I figured it out. Thanks!