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

How do you add a table to a table?

When a player joins a game I want to add a table containing their information (inventory, variables etc.) to a global table that I can then index by using the player's name. For example, if I want to access a player's coins I could do: _G.Players.[player name here].coins

But I can't quite figure out how to add a table to a global table using the name of the player. If anyone could help it would be very appreciated.

2 comments

0

I think you can do this with metatables.

0

i would use a module for this

2 answers

✓ Accepted answer
game.Players.PlayerAdded:connect(function(plr)
	_G.Players[plr.Name]["Coins"] = 0
end)

game.Players.PlayerRemoving:connect(function(plr)
	_G.Players[plr.Name] = nil
end)

2 comments

0

This will give an attempt to index a nil value error. First you need to create the table to store the "Coins" key.

0

This ended up working, but instead of indexing coins, I went with _G.Players[player.Name] = {coins = 0}

I don't for sure but maybe something like this:

local Table1 = {1,2,"Hello","This is me table lol"}
local Table2 = {4,6,7,4,"No this is me new table xD"}
local Tables = {Table1,Table2}

Does that work?

1 comment

0

Thanks for the help, but I don't think that would work as I'm trying to create a table using the players name when they join

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