Problems with hotbar system

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

1 Like

Can you tell us the exact issue you’re facing?

The hotbar either wont work or the layout messes up, so if the layout is
bow
mp5
bandage
if u equip unequip the layout switches a bit and it turns messy like
bandage bow mp5

Name the UI labels correctly and make sure your UIlistconstraint is uses alphabetical order for the layout; Since it’ll be random if you don’t have it on a forced order.

Ui isnt even implemented yet, the code is the problem

		Orders[i]=v
	end

what are you trying to do in this part of the code?

I watched a video a while ago about next, inpairs, and ipairs, and if I’m remembering this correctly, then ipairs can be used when you want things in a table (that’s an array) to stay the same order when looped through, because when looping through a table with inpairs or next, the order isn’t guaranteed to be the same every time. Since one of your problems is that the hotbar layout gets messed up, I would recommend researching about ipairs to confirm my suspicions, and to try it out and see if it helps. Btw I could be completely wrong about this so don’t quote me on it.

puting the tools in the order so when the user presses a input they get the tool from the order, idk dude the code is messy and i have like no clue on how to make a custom hotbar system, i cant imagine how the inventory system will be sfter this

I only use next because i did a text and it was faster then ipairs or pairs

Ok. Just wanted to let you know. Good luck on the hotbar! :grinning: