I haven't really used Roblox Studio in a while, so this may be some really stupid, easy-to-fix problem or whatever. I'm trying to make an inventory system for a game I'm making with a friend. (which I have never done before) Anyway, I have the main inventory functions in a module script for easy use.

local inventory_functions = {}

local Frame = script.Parent

local ItemTable = Frame.Items --a folder that holds the items data in models

local AvailableSlots = {
	Frame.Inv1,
	Frame.Inv2,
	Frame.Inv3,
	Frame.Inv4,
	Frame.Inv5,
	Frame.Inv6
}

local function UpdateInventory()
	for i,v in ipairs(ItemTable:GetChildren()) do
		if v:IsA("Model") then
			local ItemSlot = Frame[tostring(v.Slot.Value)]
			ItemSlot.Item.Value = v
			ItemSlot.ItemPicture.Image = v.Icon.Value
		end
	end
end

function inventory_functions.AddToInventory(Item) --the item is passed from a click detector in another script
	local TableSize = table.getn(AvailableSlots)
	if TableSize > 0 and TableSize <= 6 then
		table.sort(AvailableSlots, function(a, b)
			return a.Name < b.Name
		end)
		for i,v in pairs(AvailableSlots) do
			local ItemClone = Item:Clone()
			ItemClone.Parent = ItemTable
			ItemClone.Slot.Value = v
			table.sort(AvailableSlots, function(a, b)
				return a.Name > b.Name
			end)
			table.remove(AvailableSlots, table.find(AvailableSlots, tostring(v)))
			UpdateInventory()
			break
		end
	else
		warn("Inventory is full, or something has gone horribly wrong.")
	end
end

function inventory_functions.RemoveFromInventory(Item) --passed from separate short right click detection code 
	local TableSize = table.getn(AvailableSlots)
	if TableSize > 0 and TableSize <= 6 then
		Frame[tostring(Item.Slot.Value)].ItemPicture.Image = ""
		local SlotValue = Item.Slot.Value
		ItemTable[tostring(Item)]:Destroy()
		table.insert(AvailableSlots, SlotValue)
		table.sort(AvailableSlots, function(a, b)
			return a.Name < b.Name
		end)
		for i,v in pairs(AvailableSlots) do
			print(tostring(v))
		end
	else
		warn("Code has been owned (not good)")
	end
end

return inventory_functions

Also, heres the click detector script that passes the first item variable just in case it has something to do with this issue

local Detector = script.Parent.ClickDetector

local item = script.Parent.Parent.CupItem

local function Activate(plr)
	local Inventory = plr.PlayerGui.Inventory.Frame
	local inventory_functions = require(Inventory.InventoryModule)
	inventory_functions.AddToInventory(item)
end

Detector.MouseClick:Connect(Activate)

The inventory_functions.RemoveFromInventory() function is where my problems arise I'm fairly sure. Instead of inserting the correct image label into the table once like it's supposed to, it inserts more than one of the correct image label, thus breaking the code. I think the problem may have to do with the pairs loop in inventory_functions.AddToInventory(), but I have no idea where to begin to try and fix it. If you need any more information on this, feel free to ask. All help is appreciated!!!

2 comments

0

how many objects is in slots when you print?

0

Seven. The ones that are supposed to be there, and two of the new one