Table says it's empty even though it isn't?

In my status effects system I’m trying to get it so when a Status is removed it checks if the Character table has any status effects or not. However it always prints “Table empty” even if there is something inside the table?

function StatusEffects:RemoveStatusEffect(StatusEffect)
	StatusEffects.Affected[self.Humanoid.Parent.Name][StatusEffect] = nil
	self.DeathConnection:Disconnect()
	if (StatusEffects.Affected[self.Humanoid.Parent.Name][1]) then
		print("Table not empty")
	else
		print("Table empty", StatusEffects.Affected)
	end
	setmetatable(self, nil)
	table.clear(self)
	table.freeze(self)
end

You can’t index a dictionary, might as well replace it with

if (StatusEffects.Affected[self.Humanoid.Parent.Name]) then

It just prints that the table is not empty now?

If I try and use this it still just prints that the table is empty?

if (#StatusEffects.Affected[self.Humanoid.Parent.Name] > 0) then
	print("Table not empty")
else
	print("Table empty", StatusEffects.Affected)
end

You cannot get the length of a dictionary, it will just return 0.

If you wanted to, try this out:

function dictlen(dictionary)
    local len = 0
    for k, v in pairs(dictionary) do
        len += 1
    end
    return len
end

The reason why you cannot get the length directly is because an array stores data in indices as opposed to dictionary in which stores data in keys.

2 Likes

That makes a lot more sense now. Thank you, this solved my issue!

Of you can just use next(tab,nil) and verify this isn’t empty. No need to iterate over the whole table.

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