Question 1:
FindFirstChild is a method that is used to find a child. It returns the first child found if there is one or returns nil if there is no child of that name. FindFirstChild should be used if you're not sure if there is a child with a name you want to find in the parent. It also has a recursive argument which can be used to check the parent's children and children's children for the child you're looking for. This is useful if you don't know where a certain instance is in the parent.
GetChildren is a method that is used to essentially get the children of a parent. GetChildren returns a table with all the children of the parent in it. This is generally used with for loops, but I'll get to that later.
Finally, WaitForChild is essentially similar to FindFirstChild, except it yields (waits) for the child to become available instead of returning nil. This can be good if you know exactly where the child is but are not sure if it will be available instantly. A drawback to this is that if there is no child with the name in the parent, the script will yield infinitely and this makes debugging harder.
Question 2:
Generic for loops are for loops that take advantage of iterators. Iterators are functions that return the next set of values each time it's called. Generic for loops are usually used for looping through tables, similar to how numeric for loops work (for i = j,k do).
A common example of the use of generic for loops is when you want to manipulate multiple objects. Let's say we want to change all the parts in the workspace with the name "Red" to a different color, we can use generic for loops to go through each child of the workspace and check if they're parts and if their name is "Red", then we can manipulate their BrickColor property to Bright red.
for i,v in pairs(workspace:GetChildren()) do
if v:IsA("BasePart") and v.Name == "Red" then
v.BrickColor = BrickColor.new("Bright red")
end
end
The code above can also be expressed with a numeric for loop:
local children = workspace:GetChildren()
for i = 1,#children do
if children[i]:IsA("BasePart") and children[i].Name == "Red" then
children[i].BrickColor = BrickColor.new("Bright red")
end
end
Question 3:
The term you are looking for here is an anonymous function. An anonymous function is technically the same as a normal function, except that you can't call it through a variable, this is why it's known as an anonymous function.
Essentially, you could consider this as wrapping a function inside the connection line of an event.
Take a look at the following code blocks below:
workspace.Part.Touched:connect(function(hit)
print(hit.Name)
end)
function onTouch(hit)
print(hit.Name)
end
workspace.Part.Touched:connect(onTouch)
The first code block is significantly shorter than the second and is a bit different, but both code blocks perform the same thing as one another.
Finally, the hit inside the function(hit) here is a variable for an argument. You can assign variables to arguments for use inside the function. Arguments are usually set by an event. For example, the hit argument is set by ROBLOX's Touched event. But, for functions not connected to an event, you can set these arguments yourself.
Let's look at this function that isn't connected to an event:
function multiply(a,b)
return a*b
end
print(multiply(2,5))
See how you can set up arguments yourself? Sometimes, you can relate some problems in scripting with math. Relating the above problem to math, you could be given a question like this in maths:
If a is 5 and b is 2, what is the value of a multiplied by b?
This is basic algebra. Since we have the variable a as 5 and the variable b as 2, a multiplied by b is essentially 5 multiplied by 2, which equals 10!
My answer should have answered all of your questions, but if you're still unsure about some parts, leave a comment below my answer and I'll try and explain that part to you again.
I hope my answer helped you. If it did, be sure to accept it.
6 comments
You should include how 'hit' for Touched events is a built in parameter that Roblox sets. good answer though.
Alright, thanks for the tip!
Thanks for this answer, and I was wondering if sometime you could party me and I could ask you these questions in Party Chat. If so, give me some notice and I'll try to be on. Thanks!
There were some extra questions, and I thought that party chat would be the best area to do it. 3 hours notice or more is good.
(I meant you could send me a party invite on ROBLOX)
NICE ANSWER