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

How do you add a table to a table?

Asked by 6 years ago

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.

0
I think you can do this with metatables. hiimgoodpack 2009 — 6y
0
i would use a module for this User#5423 17 — 6y

2 answers

Log in to vote
-1
Answered by 6 years ago
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)
0
This will give an attempt to index a nil value error. First you need to create the table to store the "Coins" key. 2eggnog 981 — 6y
0
This ended up working, but instead of indexing coins, I went with _G.Players[player.Name] = {coins = 0} TheEnderShot 14 — 6y
Ad
Log in to vote
0
Answered by 6 years ago

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?

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 TheEnderShot 14 — 6y

Answer this question