Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Hashtags in front of variables?

Hey, I'm new to Lua but not to programming and just have a quick question. I've seen a few scripts in which there is a hashtag in front of a variable, for example...

if #players > 0 then
local randomplayer = players[math.random(1,#players)]

This is part of a script that selects out a random player, is this statement returning that #players is an integer. Or is it creating an array or something?

Thanks in advance.

2 answers

✓ Accepted answer

The # operator returns the amount of items in a particular table. That's why #[table] (in this case, #players)is an integer.

Some examples:

Table1 = {"HI", "ok", "item"}
Table2 = {324, 523, 64, 2345}

print(#Table1)
print(#Table2)

In the output, you'll get:

3

4


So in your script, it's basically saying:

if #players > 0 then

If the number of players is greater than 0, then...

local randomplayer = players[math.random(1,#players)]

The randomplayer will be a random player between the first index and the last index of the players table.

players would actually be a table. A hashtag before a string or table returns the length of that string or table. For example:

local tab =  {"one",2,"3"}
local string = "I'm a string!"

print(#tab) ---------------> 3
print(#string) ---------------> 13
print(#"apple") ---------------> 5

2 comments

0

Thanks guys, its put my mind at rest. Much more simple that I thought it would turn out to be.

0

np

This discussion is preserved as it was. New answers, comments, and votes are closed.