Hello developers!
I am having trouble with making a custom hotbar. Here is my main script:
--//Variable
local MaxSlots = 7
local NumToWord = {
[1] = "One",
[2] = "Two",
[3] = "Three",
[4] = "Four",
[5] = "Five",
[6] = "Six",
[7] = "Seven",
}
--//Module
local module = {}
function module:Initiate()
game:GetService("StarterGui"):SetCoreGuiEnabled(Enum.CoreGuiType.Backpack, false)
local conns = {}
Player.CharacterAdded:Connect(function(Character)
local HotBarGui = PlayerGui.Controllers.Gui.UI.HotBarController.HotBarGui
HotBarGui.Enabled = true
local MFrame = HotBarGui.MFrame
local Backpack = Player:WaitForChild("Backpack")
local Tools = Backpack:GetChildren()
local function updateHotBar()
for i, child in pairs(HotBarGui:GetChildren()) do
if child:IsA("ImageButton") then child:Destroy() end
end
for i, tool:Tool in pairs(Tools) do
if i > MaxSlots then return end
local Clone = Slot:Clone()
--Clone.Image = "rbxassetid://"..tool.Icon.Value
Clone.Order.Text = i
Clone.Parent = MFrame
Clone.Visible = true
if tool.Parent == Character then
Clone.Transparency = 0.25
end
print("Slot cloned.")
conns[Clone] = UIS.InputBegan:Connect(function(Input, GP)
if GP then return end
if Input.KeyCode == Enum.KeyCode[NumToWord[i]] then
CCS_Remotes.EquipTool:FireServer(tool, tool.Parent)
end
end)
end
print(Tools)
end
updateHotBar()
--[[conns["Added"] = ]]Backpack.ChildAdded:Connect(function(child)
if not table.find(Tools, child) then
table.insert(Tools, child)
updateHotBar()
end
end)
--[[conns["Removed"] = ]]Backpack.ChildRemoved:Connect(function(child)
if child.Parent ~= Character then
table.remove(Tools, Tools[child])
updateHotBar()
end
end)
--[[conns["CharAdd"] = ]]Character.ChildAdded:Connect(function(child)
if child:IsA("Tool") and not table.find(Tools, child) then
table.insert(Tools, child)
updateHotBar()
end
end)
--[[conns["CharRemoved"] = ]]Character.ChildRemoved:Connect(function(child)
if child.Parent ~= Backpack then
table.remove(Tools, Tools[child])
updateHotBar()
end
end)
--[[conns["a"] = ]]CCS_Remotes.EquipTool.OnClientEvent:Connect(function()
for x, slot in pairs(MFrame:GetChildren()) do
if slot:IsA("ImageButton") then
local tool = Tools[tonumber(slot.Order.Text)]
print(tool, slot.Order.Text)
if tool.Parent ~= Character then
slot.Transparency = 0.5
else
slot.Transparency = 0.25
end
end
end
end)
end)
Player.CharacterRemoving:Connect(function()
for i, connection in pairs(conns) do
connection:Disconnect()
conns[i] = nil
end
end)
end
return module
With that, the remote-recieving end on the server for the equiping:
function module:EquipTool(Player, Tool, Parent)
local Character = Player.Character
if EquipDebounces[Player] then return end
EquipDebounces[Player] = true
if Character then
if Parent ~= Character then
local Humanoid:Humanoid = Character.Humanoid
Humanoid:UnequipTools()
Humanoid:EquipTool(Tool)
else
Tool.Parent = Player.Backpack
end
end
CCS_Remotes.EquipTool:FireClient(Player)
task.delay(0.5, function()
EquipDebounces[Player] = nil
end)
end
I test and find out that 2 extra slots get added for no reason exactly 10 seconds after. I have nothing relating to tools yet nor adding/removing.
Please help and suggest anything about my code!