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

Custom Inventory Cloning same thing twice?

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

Basically, I'm making a Custom Inventory Gui. Theres a bug where it shows 2 of the same item, even though I don't want it to.

I've tried clearing the 2 tables in this script before the cloning process. I've tried adding wait() arguments (that just made things worse lol).

this is the script:

local player = game.Players.LocalPlayer
local character = player.Character
local items = {}
local buttons = {}
local equipped = script.Parent.Handler.Equipped
local selected = script.Parent.Handler.Selected
local location = script.Parent.Handler.Location
game.StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack,false) -- Makes the original backpack gui invisible

player:WaitForChild("Backpack")

script.Parent.Parent.Parent:WaitForChild("PlayerMain")

function search(location)
    for i,v in ipairs(location: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 Delete()
    for i,v in ipairs(items) do
        table.remove(items,v)
        v:Destroy()
    end
    for i,v in ipairs(buttons) do
        v:Destroy()
    end
end

function Add()
    for i,v in ipairs(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.Outline
        button.BorderSizePixel = 3
        table.insert(buttons,button) -- Inserts the button to our table "buttons"
        button.MouseEnter: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
                if button:GetAttribute("ItemEquipped") == false then 
                    button.BorderColor3 = Color3.fromRGB(255,0,0)
                end
                script.Parent.Frame.ItemStats.ItemName.Text = v.Name -- Sets the TextLabel's Text to the name of the tool/button
                script.Parent.Frame.ItemStats.ItemLevel.Text = "Lv. "..v:GetAttribute("Level") 
                script.Parent.Frame.ItemStats.ItemDesc.Text = ""..v:GetAttribute("Description")
                script.Parent.Frame.ImageLabel.Image = v.TextureId -- Sets the image label's image to the texture id of the tool
                selected.Value = v
                location.Value = v.Parent
            end
        button.MouseLeave:Connect(function()
            if button:GetAttribute("ItemEquipped") == false then 
                button.BorderColor3 = Color3.fromRGB(25,25,25)
            end
            script.Parent.Frame.ItemStats.ItemName.Text = "(Nothing)"
            script.Parent.Frame.ItemStats.ItemLevel.Text = "Lv. NaN"
            script.Parent.Frame.ItemStats.ItemDesc.Text = "No Description"
            script.Parent.Frame.ImageLabel.Image = "rbxassetid://0"
            selected.Value = nil
            location.Value = nil
        end)
        button.MouseButton1Click:Connect(function()
            if equipped.Value == nil or equipped.Value ~= selected.Value then -- Just the same as the last one
                character.Humanoid:UnequipTools() -- Forces the player to unequip the tool that they equipped
                if location.Value == player.Backpack then
                    character.Humanoid:EquipTool(selected.Value)
                    equipped.Value = selected.Value
                    button:SetAttribute("ItemEquipped",true)
                    button.BorderColor3 = Color3.fromRGB(0,255,0)
                end
            else
                character.Humanoid:UnequipTools()
                equipped.Value = nil
                button.EquipCheck.Visible = false
                button:SetAttribute("ItemEquipped",false)
                button.BorderColor3 = Color3.fromRGB(25,25,25)
                end
            end)
        end)
    end
end

function backpackRefresh()
    items = {}
    Delete()
    search(game.Players.LocalPlayer.Character)
    search(game.Players.LocalPlayer.Backpack)
    Add()
end

function FrameVis()
    if script.Parent.Visible == true then
        script.Parent.Parent.Frame.Visible = true
        script.Parent.Parent.Parent.PlayerMain.Dark.Main.Visible = false
        script.Parent.Parent.Parent.PlayerMain.Dark.StatHUD.Visible = false
    elseif script.Parent.Visible == false then
        script.Parent.Parent.Frame.Visible = false
        script.Parent.Parent.Parent.PlayerMain.Dark.Main.Visible = true
        script.Parent.Parent.Parent.PlayerMain.Dark.StatHUD.Visible = true
    end
end

Delete()
backpackRefresh()

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

character.ChildAdded:Connect(backpackRefresh)
character.ChildRemoved:Connect(backpackRefresh)

FrameVis()

script.Parent:GetPropertyChangedSignal("Visible"):Connect(FrameVis)

this is the few parts that this script interacts with:

a peek on the explorer

Picture of the bug (in case you don't understand what i mean)

0
no errors in the dev console btw NGC4637 602 — 2y
0
and, said script is a local script. It is called "InventoryHandler", you can see in the image i put NGC4637 602 — 2y

1 answer

Log in to vote
0
Answered by
NGC4637 602 Moderation Voter
2 years ago

nvm, this was never the backpack scripts problem, my items just somehow duped. I knew after I checked the backpack for any extra items.

lel

Ad

Answer this question