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

A little help about table.insert?

Asked by 5 years ago
Edited 5 years ago

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

Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

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!
0
Ah, I got it all figured out now, thank you! LOADING321 12 — 5y
0
Sorry, another question. How would I get the inserted value within the table without a variable or how would I set a variable? LOADING321 12 — 5y
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` EpicMetatableMoment 1444 — 5y
0
Here is some extra information you can read on arrays and dictionaries. http://lua-users.org/wiki/TablesTutorial EpicMetatableMoment 1444 — 5y
Ad

Answer this question