Hi, I'm new to scripting backpack so can someone help me with making this code better?

--//StartUp

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

--//Properties

local Player = game.Players.LocalPlayer
local UserInputService = game:GetService("UserInputService")
local IsOn = false
local Character = Player.Character
local Humanoid = Character.Humanoid

--//Toggle

function Equip()
    if IsOn == false then
        local Tool = Player.Backpack:FindFirstChildOfClass("Tool")
        if Tool then
            IsOn = not IsOn
            Humanoid:EquipTool(Tool)
        end
    else
        local Tool = Character:FindFirstChildOfClass("Tool")
        if Tool then
            IsOn = not IsOn
            Humanoid:UnequipTools(Tool)
        end
    end
end


Player.Backpack.ChildAdded:Connect(function(Child)
    if Child:IsA("Tool") then
        script.Parent.Image = Child.TextureId
    end
end)

Character.ChildRemoved:Connect(function(Child)
    if Child:IsA("Tool") and Child.Parent ~= Player.Backpack then
        script.Parent.Image = ""
    end
end)

Character.ChildAdded:Connect(function(Child)
    if Child:IsA("Tool") then
        script.Parent.Image = Child.TextureId
    end
end)

script.Parent.MouseButton1Click:Connect(function()
    Equip()
end)

UserInputService.InputBegan:Connect(function(input, gameProcessed)
    if gameProcessed then return end
    local Key = input.KeyCode
    if Key == Enum.KeyCode.Q then
        Equip()
    end
end)

3 comments

0

looks good as long as it does what you want it to do..

0

Did you make this code?

0

Yes