How do you detect the items that are in the hotbar and inventory?

I am trying to understand how to divide the hotbar and inventory systems. In other words, I need to know which items are in the hotbar and which are in the inventory. In roblox, the first 10 items you pick up go into the hotbar. Anything beyond 10 items will go into the inventory. You can also move items from the hotbar to the inventory at any given time.

Most of the inventory system youtube tutorials just do that classic “Download this random garbage” or “paste in this code without knowing what it does.” If I find out how to recognize hotbar items vs inventory items, I will be able to make my own inventory system easily. Any help would be appreciated!

1 Like

I dont think this is possible, if it is I haven’t seen it been done

if your making your own inventory system, you should just organise the tools in a custom manner, like you pick up something and your hotbar slots is already done, you script will need to recognise that and put it in the inventory, and for moving items, your gonna have to script that yourself aswell.

If I understood correctly, you’re talking about tools, right? If that’s the case, as far as I know, it’s not possible to tell whether a tool is in the inventory or in the hotbar — at least not with the Roblox API. But you can create a custom system for that in a fairly simple way.

You can disable the Roblox hotbar and backpack simply by using the method StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack, false)

The Roblox tool system works by changing the parent of the tools depending on their state. For example, when you equip a tool, it moves from your backpack to your character, and when you unequip it, it moves from your character back to your backpack. So by setting up the right connections, you can create a system similar to Roblox’s but fully customizable.

local StarterGui = game:GetService("StarterGui")
StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack, false)

local Players = game:GetService("Players")
local Player = Players.LocalPlayer

local Backpack = Player.Backpack

local function OnChildAddedOnBackpack(Child)
	
end

local function OnChildRemovedOfBackpack(Child)
	
end

local function OnChildAddedOnCharacter(Child)
	if Child.ClassName ~= "Tool" then return end
	
end

local function OnChildRemovedOfCharacter(Child)
	if Child.ClassName ~= "Tool" then return end
	
end

local function OnCharacterAdded(newCharacter)
	newCharacter.ChildAdded:Connect(OnChildAddedOnCharacter)
	newCharacter.ChildRemoved:Connect(OnChildRemovedOfCharacter)
end

Backpack.ChildAdded:Connect(OnChildAddedOnBackpack)
Backpack.ChildRemoved:Connect(OnChildRemovedOfBackpack)

OnCharacterAdded(Player.Character or Player.CharacterAdded:Wait())
Player.CharacterAdded:Connect(OnCharacterAdded)

In this example, I made some connections that you can use to build this custom system if you want.

1 Like

Thanks for responding! I was talking about tools so you were correct (I call them items sometimes because of other games I play so sorry if it was confusing). I am very new to inventory customizing and scripting in general. I sort of understand the code that you made here. If it isn’t too much of a bother could you please give me a rundown of how it works? I want to use this in my game and I need to have a better understanding before putting it into action.

This is how I see it. I probably misunderstood the entire thing but here is my rundown from what I see.

local StarterGui = game:GetService("StarterGui")
StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack, false) --removing the base hotbar/inventory

local Players = game:GetService("Players")
local Player = Players.LocalPlayer

local Backpack = Player.Backpack

local function OnChildAddedOnBackpack(Child) --when a tool is added to the backpack
	
end

local function OnChildRemovedOfBackpack(Child) --when a tool is removed from the backpack
	
end

local function OnChildAddedOnCharacter(Child) --when a tool is held out
	if Child.ClassName ~= "Tool" then return end --making sure it's a tool being added
	
end

local function OnChildRemovedOfCharacter(Child) --when a tool is put back into the backpack
	if Child.ClassName ~= "Tool" then return end --making sure it's a tool being removed
	
end

local function OnCharacterAdded(newCharacter) --I don't really understand anything after this :(
	newCharacter.ChildAdded:Connect(OnChildAddedOnCharacter)
	newCharacter.ChildRemoved:Connect(OnChildRemovedOfCharacter)
end

Backpack.ChildAdded:Connect(OnChildAddedOnBackpack)
Backpack.ChildRemoved:Connect(OnChildRemovedOfBackpack)

OnCharacterAdded(Player.Character or Player.CharacterAdded:Wait())
Player.CharacterAdded:Connect(OnCharacterAdded)

That’s how I see it. If you could tell me if my views of the code are correct, I would really appreciate it. Sorry for being so dumb with this code. I probably butchered half of the explanations :joy:. Thanks for helping me with the code!

basically his code just tells gives you a bunch of events to listen to detect when a item is equipped/unequipped/gets put in backpack/gets removed from backpack

very useful for a custom inventory system, but you can’t actually find where tools are in the default roblox backpack UI

For tools; If they are active on the bar, they are also in the character on the workspace, if not they are in the player’s backpack.

So, the code just detects when something is equipped, unequipped, put into the backpack, taken out of the backpack?

If detection between the inventory and hotbar isn’t possible, how do games make custom systems? What strategies do they use to make sure that the items over the hotbar limit go into the inventory?

They’re handling it manually through their scripts, probably like this:

  1. Check how many items are currently in the custom hotbar
  2. If it’s full, place the tool’s button into a separate frame (like the main inventory grid)

Custom inventory systems don’t use Roblox’s built-in inventory UI at all, they don’t interact with or reflect the default Backpack system. Its pretty common for these systems to manage item placement and order independently. like, for example, if you move an item from the hotbar to the inventory in a custom system, that change won’t sync with Roblox’s default inventory because it’s entirely detached and vice versa

So, the inventory systems wait for the hotbar to overflow in order to put the tools into the inventory? That’s a good idea. How do you detect the number of children in the backpack? I currently have it set to 7 maximum slots. Even then, it probably would still bug out when items are dropped and moved and stuff like that. I don’t know how to completely stray away from the default system. Thanks for answering my questions! I really appreciate your help :slight_smile:.

How do you detect the number of children in the backpack

For the number of children in the actual backpack in the player object (so not including the equipped tool): it would be

local toolsInBackpack = #player.Backpack:GetChildren()

for all the things in the inventory system (so including the equipped item), it would be:

local toolsInInventorySystem = #player.Backpack:GetChildren() + (if player.Character:FindFirstChildWhichIsA("Tool") then 1 else 0) -- this adds 1 if there's a tool in the character

A tool in the player’s character means that its equipped, in the player’s actual backpack object (player.Backpack) means its being stored in the inventory but its not equipped

How do people make their custom systems that stray away from the backpack? From what I know, the backpack and character with tools are the only way of holding and keeping items. How do people not use them?

they dont, they pretty much always use the backpack and character to detect when there’s tools in the inventory