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

Backpack GUI that equips using 1, 2 , 3?

Asked by 6 years ago

Ok, so i'm trying to make my Backpack GUI so it can equip when pressed on number key, and every time I have tried making it work with UserInputService, I crash the GUI. So how should I use UserInputService, and where?

local Items = {}
local frames = {}
local Equiped = nil
local Player = game.Players.LocalPlayer
local Character = Player.Character

game.StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack,false)

function Scan(location)
    for i,v in pairs(location:GetChildren()) do
        if v:IsA("Tool") then
            table.insert(Items,v)
        end
    end
end

function Update()
    print(Equiped)
    for i,v in pairs(frames) do
        v:Destroy()
    end
    for i,v in pairs(Items) do
        local sam = script.Sample:Clone()
        sam.Name = v.Name
        sam.Parent = script.Parent.Holder
        sam.ImageLabel.Image = v.TextureId
        sam.Text = v.Name
        table.insert(frames,sam)
        if Equiped ~= nil and Equiped == v then
            sam.BorderSizePixel = 2
        end
        sam.MouseButton1Click:connect(function()
            if Equiped == nil or Equiped ~= v then
                Equiped = v
                Character.Humanoid:UnequipTools()
                Character.Humanoid:EquipTool(v)
            else
                Equiped = nil
                Character.Humanoid:UnequipTools()
            end
        end)
    end
end

function backpackchanged()
    Items = {}
    Scan(Character)
    Scan(Player.Backpack)
    Update()
end

backpackchanged()

Player.Backpack.ChildAdded:connect(backpackchanged)
Player.Backpack.ChildRemoved:connect(backpackchanged)

Character.ChildAdded:connect(backpackchanged)
Character.ChildRemoved:connect(backpackchanged)

Answer this question