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.
Please format your code by selecting all of your code and then clicking
or you can put it like this:
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)
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.
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.
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}}