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

What does # do?

Title says it all. What does the pound symbol do when you use it in LUA scripting?

2 answers

✓ Accepted answer

The number operator (#) returns the number of elements in a list (aka. table).

list = {"hello", 1, true};

print(#list);

--> 3

2 comments

0

Your answer was simpler and easier to understand.

0

Taslem's answer was more informative though which is more beneficial than a simple answer.

The # unary operator refers to table length (the number of things in a table being used as a list). Using it looks like this:

#list

(It is a unary operator, so syntactically it acts the same way that unary minus (negation) does, e.g., just like -x)


It counts the number of indices in a table starting at 1 until it finds a nil.

E.g., these are the lengths of the following tables:

print( #{1,3,5,-5,7} );
-- 5

print(
	#{
		[1] = 5,
		[2]=4,
		[4]=9
	}
);
-- 2, since index 3 is nil

print(
	#{
		1,
		5,
		cat=three,
		false,
		[4] = true,
		[0] = 2,
		[6] = 99
	}
);
-- 4; Counting indices 1, 2, [3] is false; [4] is true.
-- 'cat' and '0' are not indices counted by table length;
-- [6] is not counted since there is no 5

print(
	#{
		1,
		nil,
		2,
		nil,
		3
	}
);
-- Length is 1, since value at [2] is `nil`.

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