What is use of # in roblox lua

hello fellow devs, im confused at what does this logo # do in roblox lua.

currently i have no code, but i have some example that make me confused :

while #game.Players:GetPlayers() < 2 do
	print("Example")
	wait(2)
end

any help would be appriciated :slight_smile:

2 Likes

# turns a table into a number, which represents the number of child’s of the table, that’s all I know

1 Like

It is explained in this documentation page:

3 Likes

“The number of”. The #game.Players:GetPlayers() is calling for the number of objects in game.Players:GetPlayers().

2 Likes

i still didnt understand,
can you explain more?

1 Like

In short, it is an operator which measures the length of a table.

1 Like

Coding example:

local myTable = {1, 2, 3}

if #myTable == 3 then
    ...
end

# is equivalent to table.getn. table.getn is deprecated. # and table.getn will return the length of an array.

# can also get the length of a string or dictionary (with metatable __len method).

The # symbol gets the length of the table after the character. By the way, it’s called a hash or a tag (or both, hashtag).

Some examples of this symbol being used is like this:

Arrays
local array = { 1, 2, 3 }
print(`"{table.concat(array, ', ')}"`, #tab) -- "1, 2, 3" 3
-- The part in the quotation marks is what the table contains
-- The second part (the number) is the length of the table using the `#` character
Tables
local tab = { ['Money'] = 516, ['Level'] = 2 }
print(#tab) -- 2
-- table.concat() doesn't work on non-arrays.
Arrays but using Instance:GetChildren()
-- workspace
-- Folder/
-- > Part
-- > Wedge
-- > Wedge

local array = workspace.Folder:GetChildren()
print(`"{table.concat(array, ', ')}"`, #array) -- "Part, Wedge, Wedge" 3
-- I forgot if table.concat() works on arrays containing instances,
-- so this might not work.

In the code you provided, it will keep looping while the amount of players in the server is 1. It looks like this code is also used for a round-type of game. The only bad thing about this while loop is that it won’t loop again when the amount of players in the server becomes 1 the second time. You can fix this by using the following code:

while true do
    while #game.Players:GetPlayers() < 2 do
        print('Example')

        task.wait(1) -- Use task.wait() instead of wait()
    end

    repeat task.wait() until #game.Players:GetPlayers() < 2
    -- Wait until there is only 1 player in the server again.
end

This code will loop forever (because true never changes to false or nil) and will print Example every second, but only if the server has 1 player.

2 Likes

# just returns the length of what’s after it, it works with both tables and string, in tables, they invoke the __len metamethod, but why is that useful?? Well, look here:

local t = {1, 2, 3, 4, 5}

print(#t) = -- 5

Now if you try with a dictionary (or non-number-indexed arrays):

local t = {a = 1, b = 2, c = 3, d = 4, e = 5}

print(#t) -- 0

0?? There’s clearly 5 items? Well, the # operator doesn’t work here, so you need to make your own method:

local t = {a = 1, b = 2, c = 3, d = 4, e = 5}
local metatable = {
	__len = function(t)
		local count = 0
		
		for i, v in pairs(t) do
			count += 1
		end
		
		return count
	end
}
t = setmetatable(t, metatable)

print(#t) -- 5
2 Likes

Small correction:
the # operator doesn’t work on dictionaries and non-number-indexed arrays!

You should have a task.wait() in the repeat until too, or it will crash.

3 Likes

It’s just called a hash or pound. Tag and Hashtag are both incorrect and refer to two unrelated concepts.

3 Likes

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