Better understanding of how to use "#"?

I will elaborate what I mean by #.

So basically, I know that if you use them correctly, you can get a specific number of them I think? I don’t really know. (That’s why I’m asking lol) I want to know how to use them, and what else they can do, or where to put them.

I haven’t done much research because I don’t know what its called other than to call it #. I can’t find any scripts for it, so you will probably have to figure it out cuz that’s the best I can explain it.

Everything is appreciated.

The # operator is most often used to return the numeric length of a table.

If you had a table that read the following:

colors = {"Blue", "Black"}

To find the amount of items in the table, you would do:

amount = #colors -- 2

This works for any table/dictionary. A common application of this operator is finding the amount of children of an instance.

#Part:GetChildren()

The above snipped would be equal to the number of instances that are direct children of Part (in other words, inside of the part).

Let me know if you’d like further explanation!

3 Likes

or length of a string:

print(#"abcde")

output: 5

3 Likes

Correct, I left that out. Thank you.

so the # just is there and, it prints all the children of the table?

Does it print the children or the amount of children?

# is the ternary operator for the __len metamethod.

The default behavior of the # operator is to return the amount of values in a number indexed table

So when you do this:

local t = {1, 2, 3}
print(#t)

You are actually doing this:

local t = {1, 2, 3}
print(getmetatable(t).__len(t))

Of course Lua obscures this as a high-level programming language.
Additionally, you can override this metamethod to whatever you want.
Maybe you have a Bank class and you want to return the amount of accounts?

local Bank = {}
Bank.__len = function(tbl)
      -- magic code
end

Side notes:
Invoking # on strings will return the length of the string.
If the object is not a string, it will use the object’s metamethod.
If the object is a table, it will use the metamethod will the table parameter passed.

3 Likes

A ternary operator is an operator that takes 3 operands, a unary operator takes a single operand. Did you mean the latter? Additionally, __len does not work in Lua 5.1, which Luau is based off of.

3 Likes

My bad, got them mixed up as I was writing it off the top of my head.

Is there another metamethod or can it not be overloaded?

No, it just returns the length of a table or string. Tables don’t have children so I assume you mean the values stored, to do that you need to use a for loop.

for i = 1, #table do
    print(table[i])
end

The for loop goes from 1 (first index) to #table (last index) and you access the values of the table with the [] operator, where the operator takes the index you want to access.

1 Like

I don’t even know what _len is…

Another question, could I use it to see how many times a player clicks?

If you ever need to get the length of a dictionary you can use the following script I wrote.

local function dictionaryLength(dictionary)
	local count = 0
	for key, value in next, dictionary do
		count+= 1
	end
	return count
end

print(dictionaryLength({a = true, b = false, c = math.huge})) --3
1 Like

The amount, yes. In number form.

1 Like

ok… what is # useful for? I know its useful but i still dont know what else i can use it for

Getting the length of a string, getting the length of an array.

Bare in mind you can also get the length of a string via “:len()”/“string.len()”.

1 Like

and what can that do to other things, like how can i use it if… lets say checking how many players are on a certain team?

local playersInRedTeam = #game.Teams.Red:GetPlayers()

Well here’s an example regarding your scenario.

That’d provide the number of players in a team named “Red”.

1 Like

I just needed an example. I might need a few more, I don’t think I will use them though. But I will see

It gets the number of instances or variables in something, like a table. (Basically converts stuff to numbers)

That should explain all of your questions and if you have anymore they have probably been answered above.