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

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

Asked by 4 years ago
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

Log in to vote
2
Answered by
royaltoe 5144 Moderation Voter Community Moderator
4 years ago
Edited 4 years ago

[] 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"
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. royaltoe 5144 — 4y
0
edited royaltoe 5144 — 4y
0
Ah you beat me to it sahadeed 87 — 4y
0
no worries you did a good job at explaining royaltoe 5144 — 4y
Ad
Log in to vote
1
Answered by 4 years ago

()

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

0
I sent this too late check above answer sahadeed 87 — 4y

Answer this question