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

How do you make this inventory script organize the items itself?

Asked by 8 years ago
wait()
local plr = game.Players.LocalPlayer
local inv = plr.minventory -- gets the inventory items in the inventory, note i'm using DataStore to save the inventory.

for i, v in pairs(inv:GetChildren()) do
    if v:IsA('IntValue') then
        if v.Value > 0 then
            frame = script.Item:Clone()
            frame.Parent = script.Parent.materialtab
            frame.Name = 'Item'..i
            frame.amount.Text = v.Value
            frame.name.Text = v.Name
            frame.Position = UDim2.new(i/10 + i/100 + 0.01, 0, 0.05, 0)-- this is the position of the item, now how would I make it so it's in order, Copper is the 6th item in the table, and always goes on the 6th slot, even if it's the only item in the inventory.
            frame.Visible = true
        end
    end
end
-- How would I make it so if copper was the only item in the inventory, it goes to slot 1 instead of slot 6

1 answer

Log in to vote
1
Answered by 8 years ago

Track your own iterations

wait()
local plr = game.Players.LocalPlayer
local inv = plr.minventory 

local i = 0; -- The initial state of it
for _, v in pairs(inv:GetChildren()) do -- Ignore the iteration count for this
    if v:IsA('IntValue') then
        if v.Value > 0 then
            i = i + 1; -- New iteration? Count it!
            frame = script.Item:Clone()
            frame.Parent = script.Parent.materialtab
            frame.Name = 'Item'..i
            frame.amount.Text = v.Value
            frame.name.Text = v.Name
            frame.Position = UDim2.new(i/10 + i/100 + 0.01, 0, 0.05, 0)
            frame.Visible = true
        end
    end
end
Ad

Answer this question