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

how to remove recent child added ?

Asked by 9 years ago

on my inventory there is limited space but every child added creates a new button... i got a counter that is 24 but i want when the counter reaches 24 it will remove any recent child added until counter is below 24

local P = game.Players.LocalPlayer;
repeat wait() until P.Character;
local C = P.Character;
local gui = script.Parent
local menu = gui.Inventory
local folder = menu.Folder
local KeepHats = folder.KeepHats
local ply = gui.Parent.Parent
local inven = menu.Core
local openclose = gui.Button
local Button = folder.Button
repeat wait() until gui.Parent.Parent:FindFirstChild("Inventory")
function getMyPack()
    return gui.Parent.Parent.Inventory
end


function removeOldOutOfTheWay(objects)
    for _, child in pairs(objects) do
        if child.Name == "Button" then
            child:Destroy()
        end
    end
end

function UpdateInventory()
    removeOldOutOfTheWay(inven:getChildren())
if script.Parent.Inventory.Options.Counter.Value <= 24 then
    local x, y = 0, 0
    local pack = getMyPack()
    local lo = pack:getChildren()
    for _, child in pairs(lo) do

      local  neww = Button:Clone()
        neww.Position = UDim2.new(x, 0, y, 0)
        neww.Item.Value = child.Name
        neww.ID.Value = child.Value
        neww.Class.Value = child.Price.Value
        neww.Parent = inven
        x = x + 0.265
        if x > 0.8 then
            y = y + 0.17
            x = 0


end

        end
    end
end

local pack = getMyPack()
pack.ChildAdded:connect( UpdateInventory )

1 answer

Log in to vote
0
Answered by 9 years ago

Use a dictionary and workspace.DistributedGameTime. Whenever there's too much inventory, go through the dictionary and find the lowest value; remove that entry.

children = {} --dictionary; key = child, value = time added
--in ChildAdded, assuming a variable "child":
children[child] = workspace.DistributedGameTime
--When you want to remove a child:
local lowestKey = nil
local lowestValue = nil
for k, v in children do
    if not lowestValue or v < lowestValue then
        lowestKey = k
        lowestValue = v
    end
end
children[lowestKey] = nil
lowestKey:Destroy()
Ad

Answer this question