Would # means like, number of... ? Also, is their a page in Roblox Wiki which I can find things like that? It would be helpful.
#
is a prefix operator.
It's an operator (like +
or *
or .
or /
), and it's prefix (like -
in -5
or -x
) as opposed to infix (like +
in 1 + 2
).
#thing
is supposed to correspond to the length of thing
.
For strings, that means the number of characters, e.g. #("cat")
is 3
and #("")
is 0
.
For lists, that means the number of things in the list, e.g., #({1, 5, 9})
is 3
and #({})
is 0
.
For lists, it's a little complicated to say what "length" means, because of lists like this:
local t = {1, 2, 3, nil, 4}
The length of t
, ie, #t
is 3
or 5
-- don't put nil
s in lists), not 4
. That's because nil
doesn't count as an entry in the list.
Length of lists also ignores other keys, e.g.
local t = {name = "Silly", owner="blue", color="red", 99, 98}
The length of t
is now 2
(it only counts the things at t[1]
and t[2]
).
Using metatables, you can define your own definition of #
using the __len
metamethod
Tells the the number of a specific thing. e.g. #game.Players:GetChildren()
tells you how much children are in players. Lets say you have a table or array, if you want to choose something random from the table or array you could do
table = {"Hello World", 1} print(table[math.random(1,#table)]) --Chooses a random thing to print. There are 2 things to print in this case.
You can also use this technique to choose a random player:
local players = game.Players:GetChildren() --Players if game.Players.NumPlayers >= 2 then --If there are 2 or more players print(players[math.random(1, #game.Players:GetChildren())].Name) --Print the random player's name. Notice the "#" before "game.Players.GetChildren()". end
You can also find how much letters there are in a string.
local string1 = "LOLOLOLOL" local string2 = "FooFoo" print(#string1, #string2) --Print how many characters, should output "8-6"
This doesn't work for numbers. You can use string.len to find how many digits are in numbers.
local string = "1337" local num = 1337 print(string.len(string), string.len(num)) --Should print "1-1".
I Hope this helps!