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

How do I prevent Guis from cloning the same Gui twice?

Asked by
NGC4637 602 Moderation Voter
3 years ago
Edited 3 years ago

So uh I have an inventory script and a backpack gui. Everything functions however in this link you will see a weird bug where all the items cloned twice. Does anyone know how to delete extra clones?

https://gyazo.com/22c8c5ba3b43124ef87678666f3f45cf

this is the backpack script:

local player = game.Players.LocalPlayer
local character = player.Character
local items = {}
local buttons = {}
game.StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack,false) -- Makes the original backpack gui invisible

function search()
    for i,v in pairs(game.Players.LocalPlayer.Backpack:GetChildren()) do -- Find all item in a specific location
        if v:isA("Tool") then -- If the item found is a "Tool"
            table.insert(items,v) -- We're going to put all the tools found in a table.
        end
    end
end

function refresh()
    for i,v in pairs(buttons) do -- Finds all items in the table
        v:Destroy() -- Destroy 'em all
    end
    for i,v in pairs(items) do -- Finds all items in the table
        local button = script.Sample:Clone() -- clones the sample button inside the localscript
        button.Name = v.Name -- sets the cloned button's name to the name of the item
        button.LayoutOrder = i
        button.Parent = script.Parent.Handler -- Sets the parent of the cloned button to the handler
        button.Image = v.TextureId -- Sets the image of the button to the texture id of the tool
        button.ZIndex = 7
        button.BackgroundColor3 = Color3.fromRGB(50,50,50)
        button.BorderColor3 = Color3.fromRGB(25,25,25)
        button.BorderMode = Enum.BorderMode.Inset
        button.BorderSizePixel = 5
        table.insert(buttons,button) -- Inserts the button to our table "buttons"
        button.MouseButton1Click:connect(function()
            if script.Parent.Handler.Selected.Value == nil or script.Parent.Handler.Selected.Value ~= v then -- Checks if the selected value is nothing or if the selected value is not the button
                script.Parent.Frame.ItemName.Text = v.Name -- Sets the TextLabel's Text to the name of the tool/button
                script.Parent.Frame.ImageLabel.Image = v.TextureId -- Sets the image label's image to the texture id of the tool
                script.Parent.Handler.Selected.Value = v
                if script.Parent.Handler.Selected.Value ~= script.Parent.Handler.Equipped.Value then --if the selected value is not the same as the equipped value then
                    script.Parent.Handler.Location.Value = v.Parent -- Sets the value of our location to the parent of the tool whether it is in the backpack or in the character
                    script.Parent.Frame.Equip.Text = "Equip" -- Self explanatory
                elseif script.Parent.Handler.Selected.Value == script.Parent.Handler.Equipped.Value then -- If the selected value is the same as the equipped value then...
                    script.Parent.Handler.Location.Value = v.Parent
                    script.Parent.Frame.Equip.Text = "Unequip"
                end
            end
        end)
    end
end

function backpackRefresh()
    items = {}
    search(character)
    search(player.Backpack)
    refresh()
end

backpackRefresh()

player.Backpack.ChildAdded:connect(backpackRefresh)
player.Backpack.ChildRemoved:connect(backpackRefresh)

character.ChildAdded:connect(backpackRefresh)
character.ChildRemoved:connect(backpackRefresh)
0
can you put the codes in a code block? Struggage 10 — 3y
0
btw the game is treating the extra clones the same way as the normal clones. for example if the normal clone dissapears the extra also dissapears NGC4637 602 — 3y

1 answer

Log in to vote
0
Answered by
pwx 1581 Moderation Voter
3 years ago

You should clear the current items table before adding more in. On each refresh you're adding more and more of the same tool into that table.

for index,item in pairs(items) do
    table.remove(items,index)
end
-- rest of your insert item code here
0
It worked, thx NGC4637 602 — 3y
Ad

Answer this question