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

why does [] {} () matter and how can we use them?

local phrases = {"Hello!","How are you?","I love ROBLOX!"}
while wait(5) do
    local phrase = phrases[math.random(1,#phrases)]
    game:GetService("Chat"):Chat(workspace.PartName,phrase,Enum.ChatColor.White)
end**

in the math random was this [], but i would like to ask what it means and how to use them? also {} ()

2 answers

✓ Accepted answer

#[] means 'at'

For example,

phrases[1] will get your phrase AT the position one

local phrases = {"Hello!","How are you?","I love ROBLOX!"}
orint(phrases[1])

You could also say the following if you have a dictionary: We're getting the game.Players AT royaltoe the index royaltoe and myDictonary at the index dog

print(game.Players["royaltoe"]

--you can do the same for tables if you write your table like this:
myDictonary = {door = workspace.Door, dog = workspace.Dog}
print(myDictonary ["dog"])

#{} Wraps the contents of your table

Not much special about this, it just specifies where you want the table to start and to end.

myTable = { "bob", "apple", 2, game.Workspace.Part }

You can make a table inside a table like so:

myTable = { innerTable = {} }
print(myTable.innerTable) -- prints the inner table's memory address

or if you don't want to name the table you can do this:

local x = {} -- create a table named 'x'
local metaTable = {}   -- create a table to store the 'x' table
setmetatable(x, metaTable) -- puts the table 'x' inside of the table 'metaTable'
print(getmetatable(x)) --print's the table 'x''s memory address
--conceptually it looks like this: 
metaTable = { {} } -- x is inside metatable

#() Wraps the parameters of your function / specifies a function and it's parameters

Writing the function:

foo = 2  --variable with the same name as the function

function foo(parameter1, parameter2)
	print(parameter1, parameter2)
end

Since the variable foo and the function foo have the same name, how are we supposed to tell them apart? We can do that with () to tell the script that we're calling a function. Since foo has parameters we need to make sure we're sending the parameters inside ()

foo = 2 -- changing foo variable
foo(1,2) -- calling the function with the parameter
--we can also say foo(), but the parameters will be nil since we specified nothing
--so it'll print "nil nil"

4 comments

0

I forgot to mention something about {}, you can specify a metatable (table inside a table) with the brackets. I'll show you how in a second when I make my edit.

0

edited

0

Ah you beat me to it

0

no worries you did a good job at explaining

()

These classic brackets are used in functions. For example:

kill(WindowMall)

{}

These brackets typically enclose an array or class. Example:

local idiots = {“sahadeed”,”sahadeed”,”sahadeed”
}

[]

These brackets are used to find a specific item in an array using its position. For dictionaries, the position can even be a string. For normal arrays, numbers only.

local biggestidiot = idiots[math.random(1,#idiots]

Above gets a random position of idiots and makes the chosen the biggest.

Comment for any questions

1 comment

0

I sent this too late check above answer

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