A problem with table

print(#table) outputs 0 even though It’s not empty. Here’s the code:
ModuleScript:

function module:Cast(cframe:CFrame, size:Vector3, overlapParams:OverlapParams)
	local humanoids = {}
	local parts = game.Workspace:GetPartBoundsInBox(cframe, size, overlapParams)
	for _, part in pairs(parts) do
		if part.Parent:FindFirstChild("Humanoid") and not humanoids[part.Parent] then
			humanoids[part.Parent] = part.Parent
		end
	end
	return humanoids
end

LocalScript:

local humanoids = _Hitbox:Cast(cframe, size, overlapParams)
if #humanoids > 0 then
	print("Not Empty: "..#humanoids)
else
	print("Empty")
end
for _, humanoid in pairs(humanoids) do -- i know that the table's not empty because this prints out names
	print(humanoid.Name)
end

On the second script, just print out the entire humanoids table
print(humanoids)

It outputs table: 0x10f53ae0f5973b15, but i think that you misunderstood the problem. #humanoids equals 0 even if the humanoids is not empty

it’s because the table is a dictionary and not an array (it has key’s and values). # only works on array’s, (tables that don’t have keys)

2 Likes

so how can i check if humanoids dictionary is not empty?

To get the length of a dictionary do:

local function FindLength(Dict)
    local count = 0
    for _,_ in Dict do
        count += 1
    end
    return count
end

The underscores are used in place of the key and value (during the loop) since we are not using them. This function just loops through the dictionary and adds to the count every time it finds an element. Very simple.

will check if that works, one sec

1 Like

For what it’s worth, it’s even cleaner if you omit the second underscore:

for _ in Dict do
2 Likes

Sorry for the wait, I had to go away from the keyboard. Sooo…
I came up with a different solution which takes from yours.
print(#table) outputs 0 even though It’s not empty. Here’s the code:
ModuleScript:

function module:Cast(cframe:CFrame, size:Vector3, overlapParams:OverlapParams)
	local humanoids = {}
	humanoids.Amount = 0 -- New variable for counting
	local parts = game.Workspace:GetPartBoundsInBox(cframe, size, overlapParams)
	for _, part in pairs(parts) do
		if part.Parent:FindFirstChild("Humanoid") and not humanoids[part.Parent] then
			humanoids[part.Parent] = part.Parent
			humanoids.Amount += 1
		end
	end
	return humanoids
end

LocalScript:

local humanoids = _Hitbox:Cast(cframe, size, overlapParams)
if humanoids.Amount > 0 then
	print("Not Empty: "..humanoids.Amount)
else
	print("Empty")
end

What do you think?

That works fine too, the function is just a cleaner way to reuse it for any needs

1 Like

If you only want to check if the table is empty, you can use next:

if next(yourDictionary) then
    -- Not empty
else
    -- Empty
end
2 Likes

Oh that’s clever, never even thought about something like that!

1 Like

Yup but I wouldn’t use it anywhere besides here, that’s the only place.

Oh this looks the cleanest to me, will check it out.

1 Like

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