How to the logic for this type of inv system?

Hi everyone, I am currently doing a inventory system, my main Inspiration is to make it similar to this one , where inventory slots already exist (i mean, slots dont get created only when a tool is added, unlike roblox default inventory)

I am using this code currently, but i am unsure how to do the keys action, Is there any way i can recognize what Slot key (number) is being pressed and select that slot?

local current_actualized_itemindex = {}
local currentindex = 1
local view_model = player.Character

backpack_folder.ChildAdded:Connect(function(c) 
	if not table.find(current_actualized_itemindex, c) then
		--print('tool added!')
		table.insert(current_actualized_itemindex, c)
	end
end)

backpack_folder.ChildRemoved:Connect(function(c)
	--print(c)
	if c.Parent ~= view_model then
		--print(c.Parent)
		--print('unadequate parenting, proceeding')

		if table.find(current_actualized_itemindex, c) then
		--	print('tool removed!')
			table.remove(current_actualized_itemindex, table.find(current_actualized_itemindex, c))
		end
	end
end)

local function check_equip()
	if #current_actualized_itemindex == 0 then
		return false
	elseif #current_actualized_itemindex>=1 then
		return true
	end
end

You can use UserInputService to detect input from the player so you can know what key is being pressed.

Example:

local UIS = game:GetService("UserInputService")

local DataTable ={
	["One"] = 1,
	["Two"] = 2,
	["Three"] = 3,
	["Four"] = 4,
	["Five"] = 5,
	["Six"] = 6,
	["Seven"] = 7,
	["Eight"] = 8,
	["Nine"] = 9,
	["Zero"] = 0
}

UIS.InputBegan:Connect(function(input: InputObject, gameProcessedEvent: boolean) 
	if gameProcessedEvent then return end

	local PressedNumberKey = DataTable[input.KeyCode.Name]
	if not PressedNumberKey then return end
	
	print("Pressed number: ", PressedNumberKey) --Should only print the pressed number, no letters!
end)
local FoundKey = Input.KeyCode.Value - 48 --// (KeyCodes for 0 to 9 starts at 48)
	if FoundKey then
		local Slot = Hotbar:FindFirstChild(tostring(FoundKey))
		if Slot then
			UpdateSlot(Slot)
		end
	end
1 Like

Different ways to solve the same problem :person_shrugging:

Your approach is probably slightly harder for beginners/intermediates to understand

The default backpack uses a table to map each new slot number’s KeyCode Value to a callback that selects that slot. It uses UserInputService.InputBegan events and checks if that table has a callback for the pressed key’s Enum Value. For each slot number, they add the value of the Zero KeyCode for 1 through 9 with the slot number to get the right KeyCode Value for any number key.