Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

What is # ?

Asked by 9 years ago

Would # means like, number of... ? Also, is their a page in Roblox Wiki which I can find things like that? It would be helpful.

0
Thanks to those who answered. I added 1 rep to each one of you(3). alphawolvess 1784 — 9y

3 answers

Log in to vote
1
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
9 years ago

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

0
Also, I'm sure the OP could conclude this but if you want to reference it in a discussion of some sort, it is called the length operator. Lacryma 548 — 9y
Ad
Log in to vote
1
Answered by
Redbullusa 1580 Moderation Voter
9 years ago

"#" - The length of a given table, the total amount of "items" in a table.

Tutorials

Log in to vote
1
Answered by 9 years ago

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!

Answer this question