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)