How can I make a table for each character?

Hello, I’m trying to make a table for each character. I already tried searching up on devforum, but I’m unsure how to do it with

table.insert

For example,

local playerBoat = {}

local Assets = {
   Epic = object;
   Cool = object;
}

table.insert(playerBoat, {player = Assets})
print(playerBoat)

So I want it to print like SilentSuprion…Epic and Cool.
Sorry for my poor explanation I don’t how to describe it. Basically think of it like a table inside a table inside a table. lol.

Thank you.

1 Like

Use player id as the table index.

Could you maybe put some pictures for better understanding?

I don’t really know what you are talking about

This works:

playerBoat[game.Players.LocalPlayer.UserId] = Assets;
table.foreach(playerBoat[game.Players.LocalPlayer.UserId],print);

You can still use player as an index, but you are storing a lot of unnecessary information using that as an index (i.e. the WHOLE player instance) just use a number as an index when you can.

2 Likes

Basically I want a serperate table for each character. Inside the character there will be stuff inside.

Using the Player as an indexer wouldn’t hurt, plus it’s way more easier to retrieve the said tables.

Players.PlayerAdded:Connect(function(PLayer)
   playerBoat[Player] = Assets
end)

Just make sure to remove the value once they leave the game.

Players.PlayerRemoved:Connect(function(PLayer)
   playerBoat[Player] = nil
   -- The index will be removed once the value is set to nil.
end)

As for making individual tables on characters, that depends on how you arrange the tables.
In this case, i suggest just directly giving it the Assets table as the value, as shown on my last example, since giving a table with player as their only index is wasteful.

On the printing side, use table.foreach as dduck5tar said, but also add an identifier on which data you’re printing off from.

for Player,Data in next,playerBoat do
   print(Player)
   table.foreach(Data,print)
end
-- Output:

-- SlientSurpion
-- Epic object
-- Cool object
1 Like

Hey just wondering would it be possible to do a table inside a table inside a table?

It’s possible!
This can also work with arrays.

-- Assuming it's inside of Player.PlayerAdded event
local Assets = {
   Parts = {
      Flag = object,
      Body = object,
   },
   Properties = {
      Speed = 5
      -- Etc...
   }
}
1 Like

You can go as deep as you want with table->table->table->ad infinitum. There is no limit. It just takes horribly readable code to access the deepest members. There are better and much more readable and understandable ways to do it.

1 Like

Sorry for extending this posts, but I’m still curious how would I add it to a table/array? From a line of code?

It’s table.insert that could do the job.

1 Like

Sorry I meant like add it into a table inside the table.
Like

local Assets = {
   Cool = {}
}

How do I insert it into Cool. Sorry for extending it again.

Ah, that just would require to give it a name as the index:

Assets.Cool.Epic = {}
1 Like

table.insert(Assets.Cool, Value)
If you want it to be an array.
Assets.Cool.Key = Value
If you want it to be a dictionary.

2 Likes