Hey, I have a custom hotbar and backpack system. Yet there is a visual bug and I am unsure how to fix it.
The bug is, that whenever I unequip a tool from my backpack, the text/image displaying the tool stays there rather than it going away/being blank.
Here is my code:
game:GetService("StarterGui"):SetCoreGuiEnabled(Enum.CoreGuiType.Backpack, false)
local Players = game:GetService("Players")
local uis = game:GetService("UserInputService")
local player = Players.LocalPlayer
local char = player.Character
local bp = player.Backpack
local hum = char:WaitForChild("Humanoid")
local frame = script.Parent
local template = script.Template
local equipped = 0.3
local unequipped = 0
local inputKeys = {
["One"] = {txt = "1"},
["Two"] = {txt = "2"},
["Three"] = {txt = "3"},
["Four"] = {txt = "4"},
["Five"] = {txt = "5"},
["Six"] = {txt = "6"},
["Seven"] = {txt = "7"},
["Eight"] = {txt = "8"},
["Nine"] = {txt = "9"},
["Zero"] = {txt = "0"},
}
local inputOrder = {
inputKeys["One"],inputKeys["Two"],inputKeys["Three"],inputKeys["Four"],inputKeys["Five"],
inputKeys["Six"],inputKeys["Seven"],inputKeys["Eight"],inputKeys["Nine"],inputKeys["Zero"],
}
function handleEquip(tool)
if tool then
if tool.Parent ~= char then
hum:EquipTool(tool)
else
hum:UnequipTools()
end
end
end
function create()
local toShow = #inputOrder
frame.Visible = true
for i = 1, #inputOrder do
local value = inputOrder[i]
local clone = template:Clone()
clone.Parent = frame
clone.Label.Text = value["txt"]
clone.Name = value["txt"]
clone.Visible = true
clone.ImageTransparency = unequipped
local tool = value["tool"]
if tool then
clone.Tool.Image = tool.TextureId
if tool.TextureId == "" then
clone.ToolName.Text = tool.Name
end
end
clone.MouseButton1Down:Connect(function()
for key, value in pairs(inputKeys) do
if value["txt"] == clone.Name then
handleEquip(value["tool"])
end
end
end)
end
template:Destroy()
end
function setup()
local tools = bp:GetChildren()
for i = 1, #tools do
for i = 1, #inputOrder do
local value = inputOrder[i]
if not value["tool"] then
value["tool"] = tools[i]
break
end
end
end
create()
end
function adjust()
for key, value in pairs(inputKeys) do
local tool = value["tool"]
local icon = frame:FindFirstChild(value["txt"])
if tool then
icon.Tool.Image = tool.TextureId
if tool.TextureId == "" then
icon.ToolName.Text = tool.Name
if tool.Parent == char then
icon.ImageTransparency = equipped
else
icon.ImageTransparency = unequipped
end
else
icon.Tool.Image = ""
icon.ImageTransparency = unequipped
end
end
end
end
function onKeyPress(inputObject)
local key = inputObject.KeyCode.Name
local value = inputKeys[key]
if value and uis:GetFocusedTextBox() == nil then
handleEquip(value["tool"])
end
end
function handleAddition(adding)
if adding:IsA("Tool") then
local new = true
for key, value in pairs(inputKeys) do
local tool = value["tool"]
if tool then
if tool == adding then
new = false
end
end
end
if new then
for i = 1, #inputOrder do
local tool = inputOrder[i]["tool"]
if not tool then
inputOrder[i]["tool"] = adding
break
end
end
end
adjust()
end
end
function handleRemoval(removing)
if removing:IsA("Tool") then
if removing.Parent ~= char and removing.Parent ~= bp then
for i = 1, #inputOrder do
if inputOrder[i]["tool"] == removing then
inputOrder[i]["tool"] = nil
break
end
end
end
adjust()
end
end
uis.InputBegan:Connect(onKeyPress)
char.ChildAdded:Connect(handleAddition)
char.ChildRemoved:Connect(handleRemoval)
bp.ChildAdded:Connect(handleAddition)
bp.ChildRemoved:Connect(handleRemoval)
setup()
Help is appreciated.