So i am trying to make a custom hotbar but im running into a problem, im trying to update the hotbar each time an item gets removed and added from player backpack so the hotbar stays updated always incase an item or something gets removed, i cant figure out a solution so i came here for help after 2 days
code with what im trying to make
local function UpdateHotBar(Tool)
if Tool.Parent ~= Character then
for i,v in next, Client.Backpack:GetChildren() do
Orders[i]=v
end
end
end
Client.Backpack.ChildAdded:Connect(UpdateHotBar)
Client.Backpack.ChildRemoved:Connect(UpdateHotBar)
my entire code if needed:
local PlayerService = game:GetService("Players")
local UserInputService = game:GetService("UserInputService")
local ReplicatedStorageService = game:GetService("ReplicatedStorage")
local TweenService = game:GetService("TweenService")
local Client = PlayerService.LocalPlayer
local Mouse = Client:GetMouse()
local Character = Client.Character or Client.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")
local Inputs = {One = 1, Two = 2, Three = 3, Four = 4, Five = 5, Six = 6}
local HotbarSlots = {"One","Two","Three","Four","Five","Six"}
local Orders = {One = nil, Two = nil, Three = nil, Four = nil, Five = nil, Six = nil}
local HotBar = script.Parent:WaitForChild("Hotbar")
local CurrentOrder
local HotbarSelectionCoolDown = false
game:GetService("StarterGui"):SetCoreGuiEnabled(Enum.CoreGuiType.Backpack, false)
local function UpdateHotBar(Tool)
for i,v in next, Client.Backpack:GetChildren() do
Orders[i]=v
end
end
UserInputService.InputBegan:Connect(function(Input, GameProcessed)
if not HotbarSelectionCoolDown then
HotbarSelectionCoolDown = true
delay(0.2, function()
HotbarSelectionCoolDown = false
end)
local Action = HotbarSlots[Inputs[Input.KeyCode.Name]]
if Action and Input.KeyCode == Enum.KeyCode[Action] and not GameProcessed then
if Character:FindFirstChildOfClass("Tool") then
if CurrentOrder == Inputs[Input.KeyCode.Name] then
Humanoid:UnequipTools()
else
local Tool = Orders[Inputs[Input.KeyCode.Name]]
Humanoid:UnequipTools()
Humanoid:EquipTool(Tool)
Orders[Inputs[Input.KeyCode.Name]] = Tool
CurrentOrder = Inputs[Input.KeyCode.Name]
end
else
local Tool = Orders[Inputs[Input.KeyCode.Name]]
Humanoid:UnequipTools()
Humanoid:EquipTool(Tool)
Orders[Inputs[Input.KeyCode.Name]] = Tool
CurrentOrder = Inputs[Input.KeyCode.Name]
end
end
end
end)
Client.Backpack.ChildAdded:Connect(UpdateHotBar)
Client.Backpack.ChildRemoved:Connect(UpdateHotBar)
I’m hoping my code isnt too messy, this is for my survival game and im hoping to get it to work if the user equips the item and also if it permantently gets removed