Inventory system doesn't' work?

Heya folks, as you can tell by the title an inventory system with a module I made isn’t working. I tested it and changed a ton of things but for some reason, the inventory table is always empty and it’s filled up only within the module? Not sure what’s happening here, i’ve checked the other posts but none of them solved my problem. Help is appreciated.

inventory = {
	
}

invfunction = {}

function invfunction:Getsize()
	return #inventory
end

function invfunction:AddItem(plr,object,counter)
	if not plr then
		warn("Not a player, returning")
		return
	end
	
	if not inventory[plr] then
		print("Created inventory, inventory didn't exist")
		inventory[plr] = {}
	end
	
	for i,v in pairs(inventory[plr]) do
		if v.item == object then
			v.count = v.count + counter
			print("Added "..counter.." Count to "..object)
			print(inventory[plr])
			print(#inventory)
			return
		end
	end
	--inventory[plr][object] = inventory[plr][object] + counter
	table.insert(inventory[plr],{item = object,count = 1})
	--inventory[plr][object] = 1
	print(object.." Did not exist, created value")
end
function invfunction:AddPlayer(plr)
	if not plr then
		warn("Not a player, returning")
		return
	end
	
	if inventory[plr] then
		warn("Player already exists, returning")
		return
	end

	inventory[plr] = {}
	print("Created inventory")
end
function invfunction:Get(plr)
	if not plr then
		warn("Not a player, returning nil")
		return nil
	end
	if not inventory[plr] then
		warn("Player doesn't exist, returning nil")
		return nil
	end
	return inventory[plr]
end

return invfunction

Like I said, when.I print the results in the module script, it shows them but outside it has no values inside the inventory table.

I have a few questions. For one, why are you aiming to use the player as a key? “inventory[plr.UserID] = {}” would hypothetically make it easier on things, not sure though? Also why are you using : as syntax for the functions? You could hypothetically place the functions within the inventory table and use ‘self’ using : I think.

Perfect chance for oop though lol. I’ll look more into it when I can get on studio.

Right now at the moment i’m trying to get the system to work before I consider things like security and User IDS (would make data store a bit easier).

Not sure how to use self :l

Either way i’m looking for a way to solve my issue, not clean and change my code without solving the problem.

EDIT: so it turns out the issue is related with the fact that it’s a dictionary but it still doesn’t change the fact that the module is returning an empty table. I think it’s this portion of the code that’s the issue:

function invfunction:Get(plr)
	if not plr then
		warn("Not a player, returning nil")
		return nil
	end
	if not inventory[plr] then
		warn("Player doesn't exist, returning nil")
		return nil
	end
	return inventory[plr]
end

I figured out the issue is that clients and servers have different module scripts, whoops. So if anyone has this same issue you need to pick between handling the module from the clients or server

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