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

How would I add a table to a table, and be able to iterate through them?

Asked by 3 years ago

I've been fooling around with this for a little while and I am a bit stumped. How would I create a table, insert players into that table when they join, and then insert another table inside the players tables to be able to iterate through them?

local players = {}
game.Players.PlayerAdded:Connect(function(player)
    table.insert(players,player)
end)

local function GenerateInventory()
    local InventoryTable = {
        Item1 = nil,
        Item2 = nil,
        Item3 = nil
    }
    table.insert(players,InventoryTable)
end
local function GenerateItem()
    local Table = {
        Count = 1,
        Durability = 100,
        Cost = 50
    }
    --Now how would I insert this into the table, replacing the first Item# that == nil with the table?
end

-- Should look like this
local players = {
    InventoryTable = {
        Item1 = {
            Count = 1,
            Durability = 100,
            Cost = 50
        },
        Item2 = nil,
        Item3 = nil
    }
}
--Now lets say I need to find out how much durability the current item has. How would I iterate through these tables to find the item I am using, and how much durability it has?

0
use PlayerAdded event, then do something like players[PlrAdded.UserId] = {TABLE} RedCoatOracuda 35 — 3y

1 answer

Log in to vote
1
Answered by 3 years ago

Seems I misunderstood the question in my answer.

You have no way to know what player table is who, So i reccomend something like players[Player.UserId] = {TABLE}, And then to set an item value inside the table you do something like players[Player.UserId].inventory.item1 = {TABLE}

As for your last question, Yes; Likely. Unless you're storing something that says your current item is whatever slot, so it can jump to that slot for you without triyng to figure out where it is.

For the rendering purposes and what not, i'd do

for item_,item in pairs(Player.inventory) do
    SlotGui:GetChildren()[item_].Durability.Text = item_.durability
end

Just psuedocode, but it would probably be something along those lines.

0
Ill ponder around and see if I can get something working. This isn't really an exact answer so I won't accept it as one, however I have a better understanding of how to approach it. Thanks for that WizyTheNinja 834 — 3y
Ad

Answer this question