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

Can someone explain what these are used for?

Asked by
KNOSIS 8
5 years ago

so ive made this small random number thingy, and ive noticed that when i print whats in the table i always have to put a 1 in it, and i have to use this symbol (#).

can someone explain what the purpose of the one is for, and what the # symbol is for, and what it does?

here the script

`` myt = {1,2,3,4,5,6,7,8,9} rng = Random.new()

myt = {1,2,3,4,5,6,7,8,9}
rng = Random.new()


print(rng:NextInteger(1,#myt))

3 answers

Log in to vote
0
Answered by 5 years ago

It gets the length of the table.

For instance:

local tab = {1, 2 , 5. "yes"} --4 items

print(#tab) --> prints 4

The reason why you needed that is because so you can use randomness properly. If we need to get the number of things in a table then have the first parameter for Random.new() be 1, and then you could print a random value in the table

You can store the #table value in a variable too.

local tab = {1, 2 , 5. "yes"} --4 items

local tabLength = #tab
print(tablLength) --> print 4 

You can also use the # operator on strings, I'm not sure why but it works for a random reason. there is a str.length function though and it is advised to use that instead.

Ad
Log in to vote
0
Answered by 5 years ago

So the 1 is the minimum for the random and the #myt is the max.

The # symbol is used for getting the length of something. It can be a string, a talbe, etc.

Example:

test = "test"

print(print(#test))

-- >>>> 4
Log in to vote
0
Answered by 5 years ago

If you look at the DevHub for the Random datatype, you can see all the functions that come with it. (https://developer.roblox.com/en-us/api-reference/datatype/Random) If you look at the Random:NextInteger() function you can see it requires a minimum and a maximum, which is what you are supplying when you say...

print(rng:NextInteger(1,#myt))

As for the # symbol, that will give you the length of an array. The length of your myt array is 9.

local a = {1,2,3}
print(#a) -- 3

local b = "hello"
print(#b) -- 5

Answer this question