Help with inventory system

Hello, I’ve been trying to solve this for three threads, but none of them really helped me or solved my problem.

The problem is my inventory system, I’ve been a beginner programmer for about 5 or 6 months, and even so I still haven’t managed to finish my game’s inventory system.

What I really want to achieve is an inventory system that shows the items to the player, and that can only be equipped one item at a time (that is, there will not be a hotbar, it will just be a button that will allow you to equip the item) .

What I’ve been trying to say all this time is that after three threads, I still can’t finish this inventory system, most of the time, it’s because I just can’t understand the player response code. I hope this is the last topic on this, the closest I could get to the result I wanted was with this code:

--Variables.
	local ReplicatedStorage = game:GetService("ReplicatedStorage");
	local PlayerService = game:GetService("Players");
--
	local InventoryGUI = script.Parent.Parent
	local InventoryMain = InventoryGUI.InventoryMain
	
	local ItemsUI = InventoryMain.ItemsUI
	local InventoryGrid = ItemsUI.InventoryGrid
	local ItemSlot = InventoryGrid.ItemSlot
	
	local StatusUI = InventoryMain.StatusUI
	local StatusFrame = StatusUI.StatusFrame
	local ItemFrame = StatusUI.ItemFrame
--
	local LocalPlayer = PlayerService.LocalPlayer
	local PlayerBackpack = LocalPlayer.Backpack
--
	local PlayerCharacter = LocalPlayer.Character or LocalPlayer.CharacterAdded:Wait()
	local Humanoid = PlayerCharacter:WaitForChild("Humanoid")
--

--Functions.
	local function GetPlayerData(Player)
		for i, v in pairs(PlayerBackpack:GetChildren()) do
			local ItemScriptFolder = v:WaitForChild("ScriptFolder")
			local ItemStatusModule = require(ItemScriptFolder.ItemStatus)
			
			if ItemStatusModule then
				ItemSlot.ItemDamage.Text = tostring(ItemStatusModule.ItemStatus.Damage)
				ItemSlot.ItemPicture.Image = ItemStatusModule.InventoryStatus.ItemPicture128
				
				ItemSlot.ItemPicture.MouseEnter:Connect(function()
					StatusFrame.ItemName.Text = tostring(ItemStatusModule.InventoryStatus.Name)
					StatusFrame.ItemName.TextTransparency = 0
					StatusFrame.ItemName.UIStroke.Transparency = 0
					
				StatusFrame.ItemDescription.Text = tostring(ItemStatusModule.InventoryStatus.Description)
					StatusFrame.ItemDescription.TextTransparency = 0
					StatusFrame.ItemDescription.UIStroke.Transparency = 0
					
					ItemFrame.ItemPicture.Image = ItemStatusModule.InventoryStatus.ItemPicture256
					ItemFrame.ItemPicture.ImageTransparency = 0
					
				end)
				
				ItemSlot.ItemPicture.MouseLeave:Connect(function()
					StatusFrame.ItemName.TextTransparency = 1
					StatusFrame.ItemName.UIStroke.Transparency = 1
					
					StatusFrame.ItemDescription.TextTransparency = 1
					StatusFrame.ItemDescription.UIStroke.Transparency = 1
					
					ItemFrame.ItemPicture.Image = "rbxassetid://"
					ItemFrame.ItemPicture.ImageTransparency = 1
				end)
			else
				return
			end
		end
	end
	
	GetPlayerData()
	
	PlayerBackpack.ChildAdded:Connect(function(ItemAdded)
		local SlotClone = ItemSlot:Clone()
		SlotClone.Parent = InventoryGrid
		
		local ItemScriptFolder = ItemAdded:WaitForChild("ScriptFolder")
		local ItemStatusModule = require(ItemScriptFolder.ItemStatus)

		if ItemStatusModule then
			SlotClone.ItemDamage.Text = tostring(ItemStatusModule.ItemStatus.Damage)
			SlotClone.ItemPicture.Image = ItemStatusModule.InventoryStatus.ItemPicture128

			SlotClone.ItemPicture.MouseEnter:Connect(function()
				StatusFrame.ItemName.Text = tostring(ItemStatusModule.InventoryStatus.Name)
				StatusFrame.ItemName.TextTransparency = 0
				StatusFrame.ItemName.UIStroke.Transparency = 0

				StatusFrame.ItemDescription.Text = tostring(ItemStatusModule.InventoryStatus.Description)
				StatusFrame.ItemDescription.TextTransparency = 0
				StatusFrame.ItemDescription.UIStroke.Transparency = 0

				ItemFrame.ItemPicture.Image = ItemStatusModule.InventoryStatus.ItemPicture256
				ItemFrame.ItemPicture.ImageTransparency = 0
			end)

			SlotClone.ItemPicture.MouseLeave:Connect(function()
				StatusFrame.ItemName.TextTransparency = 1
				StatusFrame.ItemName.UIStroke.Transparency = 1

				StatusFrame.ItemDescription.TextTransparency = 1
				StatusFrame.ItemDescription.UIStroke.Transparency = 1

				ItemFrame.ItemPicture.Image = "rbxassetid://"
				ItemFrame.ItemPicture.ImageTransparency = 1
			end)
		end
	end)
--

(Also, the GUI Parents & Childrens).

Yes, it’s a pretty crappy way to do the inventory system, but it’s all I could do. Thanks.

2 Likes

lacking info, all I can do is make the code more optimal.
i am baked rn so ima go to mars frfr rq

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local PlayerService = game:GetService("Players")

local InventoryGUI = script.Parent.Parent
local InventoryMain = InventoryGUI.InventoryMain

local ItemsUI = InventoryMain.ItemsUI
local InventoryGrid = ItemsUI.InventoryGrid
local ItemSlot = InventoryGrid.ItemSlot

local StatusUI = InventoryMain.StatusUI
local StatusFrame = StatusUI.StatusFrame
local ItemFrame = StatusUI.ItemFrame

local LocalPlayer = PlayerService.LocalPlayer
local PlayerBackpack = LocalPlayer.Backpack

local PlayerCharacter = LocalPlayer.Character or LocalPlayer.CharacterAdded:Wait()
local Humanoid = PlayerCharacter:WaitForChild("Humanoid")

-- Utility to create slots and set up listeners
local function createItemSlot(item)
    local slotClone = ItemSlot:Clone()
    slotClone.Parent = InventoryGrid
    slotClone.Visible = true
    
    local ItemScriptFolder = item:WaitForChild("ScriptFolder")
    local ItemStatusModule = require(ItemScriptFolder.ItemStatus)

    slotClone.ItemDamage.Text = tostring(ItemStatusModule.ItemStatus.Damage)
    slotClone.ItemPicture.Image = ItemStatusModule.InventoryStatus.ItemPicture128

    -- Displaying item details on hover
    slotClone.ItemPicture.MouseEnter:Connect(function()
        StatusFrame.ItemName.Text = ItemStatusModule.InventoryStatus.Name
        StatusFrame.ItemDescription.Text = ItemStatusModule.InventoryStatus.Description
        ItemFrame.ItemPicture.Image = ItemStatusModule.InventoryStatus.ItemPicture256
        -- Make texts and images visible
        StatusFrame.ItemName.TextTransparency = 0
        StatusFrame.ItemDescription.TextTransparency = 0
        ItemFrame.ItemPicture.ImageTransparency = 0
    end)

    -- Clearing details when not hovering
    slotClone.ItemPicture.MouseLeave:Connect(function()
        StatusFrame.ItemName.TextTransparency = 1
        StatusFrame.ItemDescription.TextTransparency = 1
        ItemFrame.ItemPicture.ImageTransparency = 1
    end)

    -- Equip functionality on click
    slotClone.ItemPicture.MouseButton1Click:Connect(function()
        Humanoid:EquipTool(item)
    end)
end

-- Populate the inventory on load
for i, item in pairs(PlayerBackpack:GetChildren()) do
    createItemSlot(item)
end

-- Update inventory on item addition
PlayerBackpack.ChildAdded:Connect(function(itemAdded)
    createItemSlot(itemAdded)
end)

1 Like

Hi, so there is a problem that every time the tool gets back in the backpack, it clones it again, any ideas on how to solve it?

yeah read the code, understand it, and follow these steps:

1. Track existing slots

Use a dictionary/table to keep track of UI slots associated with each unique item. This allows the script to check if a slot already exists before creating a new one.

2. Only create slots when necessary

Modify the event that handles new items being added to the backpack to check the tracking dictionary first. If a slot for the item already exists, update it instead of creating a new one.

3. Properly remove slots

Ensure that when an item is permanently removed from the inventory (not just moved to the character or elsewhere), its corresponding UI slot is also removed.

I believe you can fix this, don’t think its going to be that hard.

Already got it solved, thanks.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.