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.
# 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