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

How do I insert a key and value into a dictionary while the game's running?

Asked by 9 years ago
table[#table+1] = player.Name.."="..ds:GetAsync(player.userId) or 0

--attempt to concatenate a nil value

I know it's erroring because I'm inserting a string and a number obviously which doesn't make sense for a normal table. I want to create a key and value inside of a dictionary..

1 answer

Log in to vote
2
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
9 years ago

The problem here is precedence.

or is the lowest precedence, meaning the expression is interpreted as

( player.Name .. "=" .. ds:GetAsync(player.userId) ) or 0

You just need explicit parenthesis:

player.Name .. "=" .. (ds:GetAsync(player.userId) or 0)

The error should have hinted this -- it said you're concatenating nil. Concatenating is the .. operator; the only thing near a .. that could be nil is ds:GetAsync.


Aside: This isn't a dictionary. This is a list.

You should consider using table.insert instead of tab[#tab]. You also really shouldn't use the variable name table, because it will cover up the table library and maybe mess you up later.

0
Wow thank you, although I have re-done the layout of what I'm scripting, (although small) this has taught me something more about scripting. (The position of the parenthesis) <3 magiccube3 115 — 9y
Ad

Answer this question