Need help on this custom hotbar

Hey, I am making a custom Hotbar, it’s working fine but I have got an issue with it, basically, if you have an item in your backpack named “Gravity” then, if I again put a tool inside the backpack and name it exactly the same name “Gravity” it won’t show. I need help with it. Thanks.
Code:

script.Parent.Parent.Enabled = false

repeat
    
    wait()
until game.Players.LocalPlayer.PlayerGui:FindFirstChild("Loading") == nil

script.Parent.Parent.Enabled = true


local Background = script.Parent;
local Player = game.Players.LocalPlayer;
local Character = Player.Character;

local BP = Player.Backpack

local Humanoid = Character:WaitForChild("Humanoid");


Character.ChildRemoved:Connect(function(child)
    if child:IsA("Tool") and child.Parent == nil then
        local ui = Background:FindFirstChild(child.Name)
        if ui then
            fixIndexesAndSort(Background)
        end
    end
end)

local AssignedTool;

local UserInputService = game:GetService("UserInputService");

local runService = game:GetService("RunService")

local onPc = UserInputService.KeyboardEnabled

local OnPhone = UserInputService.TouchEnabled

local Equipped = false;

function EquipTool(tool)
    if tool then
        Humanoid:EquipTool(tool)
    else
        Humanoid:UnequipTools();
    end
end

local KeyDictionary = {
    
    [1] = Enum.KeyCode.One,
    [2] = Enum.KeyCode.Two,
    [3] = Enum.KeyCode.Three,
    [4] = Enum.KeyCode.Four,
    [5] = Enum.KeyCode.Five
}


function sortOut(BackpackC)
    for i = 1, #BackpackC, 1 do
        print(i)
        local Template = game.ReplicatedStorage.Template:Clone();
        Template.Parent = Background;
        Template.NumberIndex.Text = i
        local BackpackTool = BackpackC[i]
        Template.Name = BackpackTool.Name
        Template.Front.Image = BackpackC[i].ImageLabel.Image
        BackpackTool.Index.Value = i
        local NewTool = BackpackTool:Clone()
        NewTool.Parent = Template
    end
end

sortOut(Player.Backpack:GetChildren())

local LastKeyPressed = false;

function fixIndexesAndSort(background)
    
    for x, v in pairs(Background:GetChildren()) do
        if v:IsA("ImageButton") then
            v:Destroy()
        end
    end
    
    
    for i = 1, #Player.Backpack:GetChildren() do
        local tool = Player.Backpack:GetChildren()[i]
        tool.Index.Value = i
        local Template = game.ReplicatedStorage.Template:Clone()
        Template.Name = tool.Name
        Template.Parent = background
        Template.NumberIndex.Text = i
        Template.Front.Image = tool.ImageLabel.Image
    end
    
end


BP.ChildRemoved:Connect(function(child)
    if child.Parent == nil or child.Parent == game.Workspace then
        game:GetService("RunService").RenderStepped:Wait()
        fixIndexesAndSort(Background)
    end
end)

BP.ChildAdded:Connect(function(child)
    
    if (not Background:FindFirstChild(child.Name)) then
        local Template = game.ReplicatedStorage.Template:Clone()
        Template.Front.Image = child:WaitForChild("ImageLabel").Image
        Template.Name = child.Name
        Template.Parent = Background
        if Player.Character:FindFirstChildWhichIsA("Tool") then
            Template.NumberIndex.Text = #Player.Backpack:GetChildren() + 1
            wait(0.1)
            child.Index.Value = #Player.Backpack:GetChildren() + 1
        else
            Template.NumberIndex.Text = #Player.Backpack:GetChildren()
            wait(0.1)
            Player.Backpack[child.Name].Index.Value = #Player.Backpack:GetChildren()
        end
    else
        return
    end
end)

local debounce = false


UserInputService.InputBegan:Connect(function(Input, GPE)
    if GPE then return end
    if OnPhone then return end
    for i, v in pairs(KeyDictionary) do
        if Input.KeyCode == v then
            local Backpack = Player.Backpack
            local Key = i
            print(Key)
            
            
            local FoundTool = Character:FindFirstChildWhichIsA("Tool")
            
            if FoundTool then
                local InDex = FoundTool:FindFirstChild("Index")
                if InDex and InDex.Value == Key  then
                    EquipTool(nil)
                    local Tem = Background:FindFirstChild(FoundTool.Name)
                    if Tem then
                        Tem.BackgroundTransparency = 0;
                    end
                    return
                end
            end
            for a, b in pairs(Backpack:GetChildren()) do
                local Index = b:FindFirstChild("Index")
                if Index and Index.Value == Key then
                    EquipTool(nil)
                    game:GetService("RunService").RenderStepped:Wait()
                    EquipTool(Index.Parent)
                    local Tem = Background:FindFirstChild(Index.Parent.Name)
                    game:GetService("RunService").RenderStepped:Wait()
                    if Tem then
                        Tem.BackgroundTransparency = 0.5
                        for ix, dx in pairs(Background:GetChildren()) do
                            if dx:IsA("ImageButton") and (dx ~= Tem) then
                                dx.Transparency = 0
                            end
                        end
                    else
                        print("Not template found!")
                    end
                end
            end
            break
        end
    end
end)

runService.RenderStepped:Connect(function()
    if #Player.Backpack:GetChildren() == 0 then
        for x, i in pairs(Background:GetChildren()) do
            if i:IsA("ImageButton") then
                if (not Player.Character:FindFirstChildWhichIsA("Tool")) then
                    i:Destroy()
                end
            end
        end
    end
end)
2 Likes

looks like this line is what is doing it if I am correct since in your background there is already an item named “Gravity” so it just returns nil instead of adding a tool named “Gravity”

I only skimmed through it… good chance I am wrong

Yeah I know about it but I can’t get around it if I remove that line so it just ends up corrupting it.

???

with that line, it checks if there is one named gravity, if there is one, then it won’t add another. what do you mean it ends up corrupting it.

Yeah that’s what I mean, now Ima send u a screen shot but here, when a tool is equipped it gets out of our inventory, and when u unequip it, it gets in backpack again so that backpack.ChildAdded does it again. I need a solid solution for it. Screenie:

so just to be clear, when you equip the tool, it removes it from your backpack. then when you unequip it, it adds it back to your backpack adding a duplicate to your hotbar? Correct?

tool.Equipped:Connect(function(mouse)
    --code to remove it from hotbar
end)

backpack.ChildAdded:Connect(function()
    --code to add it to hotbar
end)

Yeah you get it, but that’s not how I want it, I set the transparency to 0.5 to make it feel like its equipped
.

maybe try something along the lines of

local equipped

tool.Equipped:Connect(function(mouse)
    --set equiped to this tool
end)

backpack.ChildAdded:Connect(function()
    --check of the tool being added to backpack is equipped, if so, then don't add another tool and set equipped to nil
end)

Genius. Let me try this out real quick. Thanks