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

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

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 comment

1 answer

✓ Accepted answer

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.

1 comment

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

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