Issue with own ModuleScript function

I’m currently having an issue with my ModuleScript and a own created function. When I try to autosave the data in a datastore I loop trough all values in the module script table. The following function somehow appears as a key in the table. (I minimized the Script a bit)

local Inventories = {}

function Inventories.AddItem(Player, SlotValue, ItemData)
    local PlayerUserId = "Player_".. Player.UserId
    if not Inventories[PlayerUserId] then
	Inventories[PlayerUserId] = {}
	end
	local Inventory = Inventories[PlayerUserId]
	Inventory[SlotValue] = ItemData
	return true
end

local function saveOnExit(Player)
	local PlayerUserId = "Player_".. Player.UserId
	savePlayerData(PlayerUserId)
	for PlayerUserId, data in pairs(Inventories) do
		print(PlayerUserId, #data)
                -- output --> AddItem nil
	end
end

return Inventories

I would like to have a solution where the function no longer appears as a key in the table and can be also called with require in another script.
Just for example what I mean with require in the another script:

local ModuleScript = require(Path.ModuleScript)

local function AddItem(Player, ItemData)
ModuleScript.AddItem(Player, SlotValue, ItemData)
end
1 Like

Makes sense why this is a key in the table, function Inventories.AddItem() is practically the same syntax as Inventories.AddItem = function(). Might be of use to use a nested dictionary or ignore function types.

1 Like

That explains why it is a key in the table. Now I just need a solution how I can prevent that but also can call the function from a different script. The only way to prevent that that comes to my mind is with RemoteEvents but I would like to manage that with require.

1 Like

You could use a table inside Inventories named ‘Data’ and iterate through that when you need to.

1 Like