Table that should be reading as empty is reading as containing data?

  1. What do I want to achieve?
    Fairly simple, I am working on an inventory script and am currently in the process of linking it to my DataStore.

  2. What is the issue?
    First, allow me to post the relevant portions of the script:

Here is what the default player data in my ModuleScript looks like:

local defaultPlayerData = {
	["Guide"] = 1,
	["GuideQuests"] = {
		[1] = false,
		[2] = false,
		[3] = false,
	},
	["NPC2"] = 1,
	["Inventory"] = {},
	["Money"] = 0;
}

A lot of this is just placeholder stuff, but the Inventory table is not.

Now here is the relevant portion of my ServerScript. All of this is located in a ServerSide ModuleScript placed inside ServerScriptService.

To reduce clutter, I will only post a part of the relevant function. This is inside a function called onPlayerAdded that runs via task.spawn() when the player joins and only uses the player as the parameter

-- Make sure the player and backpack are loaded in alongside the DataStore
	while true do
		local character = workspace:FindFirstChild(player.Name)
		local backpack = player:FindFirstChildOfClass("Backpack")

		if character and backpack then
			break  -- Exit the loop once both character and backpack are found
		end

		task.wait()  -- Wait before checking again
	end

	
	-- Check if the player already has inventory data from the DataStoreModule
	local hasInventory = DataStoreModule.GetDataValue(player, "Inventory")
	
	if hasInventory then
		local decodedInv = InventoryServer.getDecodedInventoryData(hasInventory)
		
		InventoryServer.recreateTableAndCloneItems(player, decodedInv)
		InventoryServer.AllInventories[player] = decodedInv
	else
		print("No inventory data was found!")
		-- Default player inventory
		InventoryServer.AllInventories[player] = {
			Inventory = {},
			Hotbar = {},
			LastStackId = 0;
		}
	end

GetDataValue is just a function that returns the corresponding value that is passed as the second argument. Whenever I get the default data for Inventory, the function returns as empty table {}.

However, whenever I run the HasInventory function and that empty table is returned, I would expect the else portion of the conditional statement to run. However, this is not the case and the first portion of the conditional runs, throwing an error stemming from the recreateTableAndCloneItems function, saying that the decodedInventory argument is Nil.

However, if it was nil, would the else portion of the conditional statement run? It is currently not working like that, and it is confusing me.

  1. What solutions have I tried thus far?

The only change to the function I have made so far is this,

if hasInventory[1] then

When there is no data in the Inventory data, this works for that, but then it does not trigger the else statement even when there is data inside the inventory table.

I am not sure how to fix it when (unless I am mistaken), the else portion should be running via an empty table being returned.

Any help is appreciated, and I can provide additional info if need be. Thanks in advance.

hasInventory will never be nil as it’s still a table even when empty.
To check if it’s empty you could just check the number of entries inside:

if #mytable>0 then -- has content
---
else
---
end

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