Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Why is this array saying there is nothing inside when there is?

			healList[v.UserId] = healBeam
			for i,v in pairs(healList)do
				print(v.Name) -- prints UserId of player
			end
			print(#healList) -- prints 0

The array has something inside as it printed out the UserId of the player, yet it says there is nothing in the array. Can anyone help me understand why this is happening?

1 answer

✓ Accepted answer

The length operator doesn't actually count the number of elements in an array. It finds the largest consecutive index starting at 1. This means that:

local array = {}
array[2] = 2
array[3] = 3
print(#array) -- will print 0, since array[1] is nil

You are using UserIds, which of course would not be consecutive. To properly count the elements in your array:

local count = 0
for _,_ in pairs(healList) do
	count = count + 1
end
print(count) -- correct number of keys

This discussion is preserved as it was. New answers, comments, and votes are closed.