Basic inventory system not working

  1. What do you want to achieve?
    I would like to make a basic inventory system that allows the player to click on a object/tool UI button to see its preview on another frame (right next to inventory frame), and click on an ‘Equip’ button to make it appear in one of the player’s inventory’s slot.

  2. What is the issue?
    Here is a recording of the interface that is not working (you can see that no tool is present on the left-side frame):
    Roblox Studio - test 2 - Roblox Studio - 15 July 2024 | Loom

  3. What solutions have you tried so far?
    So I’ve tried to think my process and code in a new way, changing scripts’ locations in the file structure. I also spoke a lot with ChatGPT, and went through several topics on the Roblox Dev Forum.
    I suspect the ‘InventoryManager’ module script to be the issue, or/and the connection between the item/tool and the inventory.

What I have done so far:

Files Structure

InventoryManager (ModuleScript)

-- ReplicatedStorage/InventoryManager.lua
local InventoryManager = {}

local inventory = {}
local equippedItem = {}

function InventoryManager:AddItem(player, item)
	if not inventory[player.UserId] then
		inventory[player.UserId] = {}
	end
	table.insert(inventory[player.UserId], {
		name = item.name,
		image = item.image,
		model = item.model
	})
end

function InventoryManager:GetInventory(player)
	return inventory[player.UserId] or {}
end

function InventoryManager:SetEquippedItem(player, item)
	equippedItem[player.UserId] = item
end

function InventoryManager:GetEquippedItem(player)
	return equippedItem[player.UserId]
end

return InventoryManager

Items (ModuleScript)

-- ReplicatedStorage/Items.lua
local Items = {}

Items.Sword = {
	name = "Sword",
	image = "rbxassetid://18492342454",
	model = game.ReplicatedStorage.Tools:FindFirstChild("Ironsmith House Sword")
}

-- Add more items as needed
-- Items.ItemName = { name = "ItemName", image = "rbxassetid://AssetID", model = "Path/to/tool" }

return Items

EquipItemHandler (Server Script)

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

local equipItemEvent = ReplicatedStorage:WaitForChild("EquipItemEvent")
local Items = require(ReplicatedStorage:WaitForChild("Items"))

local function onEquipItem(player, itemName)
	local itemData = Items[itemName]
	if itemData then
		-- Assume the tool is stored in ReplicatedStorage under a folder named "Tools"
		local tool = ReplicatedStorage:WaitForChild("Tools"):FindFirstChild(itemName)
		if tool then
			-- Clone the tool and parent it to the player's Backpack
			local toolClone = tool:Clone()
			toolClone.Parent = player.Backpack
			print(player.Name .. " has equipped " .. itemName)
		else
			warn("Tool not found for item: " .. itemName)
		end
	else
		warn("Item data not found for item: " .. itemName)
	end
end

equipItemEvent.OnServerEvent:Connect(onEquipItem)

InventoryUI (Local Script)

-- StarterGui/InventoryGui/InventoryUI.lua
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")

local InventoryManager = require(ReplicatedStorage:WaitForChild("InventoryManager"))
local equipItemEvent = ReplicatedStorage:WaitForChild("EquipItemEvent")

local player = Players.LocalPlayer
local inventoryGui = script.Parent
local inventoryFrame = inventoryGui:WaitForChild("InventoryFrame")
local equipFrame = inventoryFrame:WaitForChild("EquipFrame")
local itemsFrame = inventoryFrame:WaitForChild("ItemsFrame")
local previewImage = equipFrame:WaitForChild("PreviewImage")
local equipButton = equipFrame:WaitForChild("EquipButton")

local selectedItem = nil

--local function clearInventoryUI()
	--for _, child in pairs(inventoryFrame:GetChildren()) do
		--if child:IsA("TextButton") then
			--child:Destroy()
		--end
	--end
--end

local function createItemButton(item, index)
	local itemButton = Instance.new("TextButton")
	itemButton.Name = "ItemButton" .. index
	itemButton.Parent = inventoryFrame

	itemButton.MouseButton1Click:Connect(function()
		selectedItem = item
		previewImage.Image = item.image

		-- Debugging: Print selected item details
		print("Selected item:", selectedItem.name, "with image:", selectedItem.image)
	end)
end

local function updateInventoryUI()
	--clearInventoryUI()

	local inventory = InventoryManager:GetInventory(player)

	-- Debugging: Print inventory to ensure it's being retrieved correctly
	print("Player Inventory:", inventory)

	for index, item in ipairs(inventory) do
		createItemButton(item, index)
	end
end

equipButton.MouseButton1Click:Connect(function()
	if selectedItem then
		InventoryManager:SetEquippedItem(player, selectedItem)
		equipItemEvent:FireServer(selectedItem.name)

		-- Debugging: Print equipped item details
		print("Equipped item:", selectedItem.name, "with image:", selectedItem.image)
	else
		print("No item selected to equip.")
	end
end)

updateInventoryUI()

Thank you so much for your help!