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

A little help about table.insert?

Trying to figure out how to insert values into table like this for an example:

stat= {
	plrname = {userid = 0, gold = 0)
}

i know you use table.insert() but i haven't read that much about it

table.insert(stat, plrname)

^ that's the only thing I know, no clue how to add the userid and gold thanks for taking your time to help me out

1 answer

✓ Accepted answer

A little help about table.insert

What's wrong.

table.insert is for arrays not dictionaries. In this case you're trying to create a dictionary.

Solution

When writing a new index to a table, you can use the same notation for indexing a value but with a little extra. a[b] = c; where a is a table, b is the key, and c is the value.

Example case; a string for the key

local myTable = {}

myTable["me"] = "cool"

This would result in a table that looks like this:

{
	me = "cool"
}
Make sure to accept this answer if it solved your problem!

4 comments

0

Ah, I got it all figured out now, thank you!

0

Sorry, another question. How would I get the inserted value within the table without a variable or how would I set a variable?

0

I'm confused by your question. If you're asking how to retrieve the data inside the table containing the player's stats you would index stats with the player name and then the specific stat. stats[playername].gold

0

Here is some extra information you can read on arrays and dictionaries. http://lua-users.org/wiki/TablesTutorial

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