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

[REPOST] Why is my backpack/inventory not working properly?

Asked by 5 years ago
Edited 5 years ago

I have been trying to make a custom backpack/inventory Gui that allows players to have a better experience in my game, but it isn't quite working yet. Here is my code:

    local player = game.Players.LocalPlayer
    local character = player.Character
    game.StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack, false)
    function CreateButtons(location)
        for key, value in pairs(location:GetChildren()) do
            if value:IsA("Tool") then
                local ImageButton = Instance.new("ImageButton", script.Parent)
                ImageButton.Image = value.TextureId
                    ImageButton.MouseButton1Click:connect(function()
                      character.Humanoid:EquipTool(value)
                   end)
                end
        end
    end

    function BackpackRefresh()
        for key, value in pairs(script.Parent:GetChildren()) do
           if value:IsA("ImageButton") then
            value:Destroy()
           end
        end
        CreateButtons(character)
        CreateButtons(player.Backpack)
    end

    BackpackRefresh()
    player.Backpack.ChildAdded:Connect(BackpackRefresh)
    player.Backpack.ChildRemoved:Connect(BackpackRefresh)
    character.ChildAdded:Connect(BackpackRefresh)
    character.ChildRemoved:Connect(BackpackRefresh)

So, my code basically makes buttons for every tool it finds in a player's inventory/backpack, and it works. But there is only one problem: when the onclicked event is called and the character.Humanoid:EquipTool(value) script runs, the player doesn't have the tool equipped even though the tool has a handle. Is there something I am doing wrong? Tell me please!

Answer this question