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
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
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.