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

Hashtags in front of variables?

Asked by
Cuvette 246 Moderation Voter
9 years ago

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

Log in to vote
3
Answered by
Redbullusa 1580 Moderation Voter
9 years ago

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.

Ad
Log in to vote
1
Answered by 9 years ago

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
0
Thanks guys, its put my mind at rest. Much more simple that I thought it would turn out to be. Cuvette 246 — 9y
0
np aquathorn321 858 — 9y

Answer this question