How to do Tables inside Tables? (Not is a Datastore)

  1. I want to do like a Table inside a table with players informations, something like OOP.
    Example: 4 players (Herick, Jhon, Pietro and Lucas) enter in game. So we will create tables inside Plrs table.

Plrs = {

Plrs.Herick = {}
Plrs.John = {}
Plrs.Pietro = {}
Plrs.Lucas = {}

Plrs.Herick.Name = "Herick"
Plrs.Herick.Level = 1
Plrs.Herick.Money = 100

Plrs.Jhon.Name = "Jhon"
Plrs.Jhon.Level = 1
Plrs.Jhon.Money = 100

Plrs.Pietro.Name = "Pietro"
Plrs.Pietro.Level = 1
Plrs.Pietro.Money = 100

Plrs.Lucas.Name = "Herick"
Plrs.Lucas.Level = 1
Plrs.Lucas.Money = 100
}

Obs: I know how to create the script, like.

game.Players.PlayerAdded:Connect(function(plr)
   --...
end)

I only want to know how to put a table inside a table.
(And if need Metatable, don’t has problem.)

5 Likes

Please format your code by selecting all of your code and then clicking
Capture
or you can put it like this:
Capture


To save data in a table you can simply do this:

game.Players.PlayerRemoving:Connect(function(player)
    local leaderstats = player:WaitForChild("leaderstats")
    local PlayerKey = player.UserId
    local DataTable = {
        Level = leaderstats.Money.Value,
        Money = leaderstats.Money.Value,
        -- etc.
    }
    local Success, ErrorMessage = pcall(function()
        MyDataStore:SetAsync(PlayerKey, DataTable)
    end)
    if ErrorMessage then
        warn(ErrorMessage, "Data Cannot Be Saved")
    end
end)

Corresponding to that, you would lead data like this:

game.Players.PlayerAdded:Connect(function(player)
    local PlayerKey = player.UserId
    local Success, Data = pcall(function()
        return MyDataStore:GetAsync(Key)
    end)
    
    if Success then
        if Data then
            Level.Value = Data["Money"]
            Money.Value = Data["Money"]
        end
    else
        warn(Data, "Data Cannot Be Loaded")
    end
end)

Thanks, but you didn’t understand what I want.
I didn’t wanna to save or load data.

I only wanna do a Table/Dictionary, who have tables to all players in game.

Something like Folders in Explorer.

FolderWithAllTablesOfPlayers.SpecificPlayerFolder.Properties.

You can simply wrap all that inside one single table:

local PlayersTable = {}
game.Players.PlayerAdded:Connect(function(player)
    PlayersTable[player.Name] = {
        -- Statistics here
    }
end)

game.Players.PlayerRemoving:Connect(function(player)
    table.remove(PlayersTable, player.Name)
end)
6 Likes

BTW, “tables inside tables” isn’t really what OOP is.

Tables inside tables are just in-depth tables. The easiest way to interact and create such is as aforementioned:

local firstDepthTable = {
    secondDepthTable = {thirdDepthTable = {}} -- an optional third
}

You can add as much depth as you like but it is generally preferred to write them in two or three depths, provided that it is none of the complex situations, for instance, a large data store.

1 Like

Hey, can you show how to add table in table with table.insert() ?

table.insert(firstDepthTable, {}). However, this method does not allow it to be found easily and is one of the numbered indexes. You will have to run a for loop to find the correct table otherwise.

1 Like

You can do this
local someTable = {{1, true}, {0, false}}
Tables can have different types in the same container, including tables and functions!
You can add as many nested tables as you want
local veryLongTable = {{{{true, 1}, {false, 0}}, {false, 0}}, {false, 0}}