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

Can someone answer these basic scripting questions?

Asked by 9 years ago

Scripting Question #1: FindFirstChild + GetChildren I was looking at some scripts, and I noticed things like FindFirstChild, GetChildren and WaitForChild I can guess what they do, but I don't know when to use them. If you know, provide a short description of each and various examples of them. Thanks :).

Scripting Question #2: Generic For Loops Also looking at some scripts, I found things like: for i,v in pairs and I was scratching my head wondering "What the Pop-Tart?". Again, if possible, provide a short description of it and when it's used. Thanks again! :D.

Third and Final Scripting Question: connect, and calling functions Again looking at scripts, I noticed this: game.Workspace.Part.Touched:connect(function(hit) I understand the Touched part of that script, but not the end. (function(hit) specifically. I understand function, but not the (hit) part. That segment changes depending on the event, and I would like to learn how it works. Thank you so much!

Thank you for responding to these and helping me learn to script. I can't thank you enough.

3 answers

Log in to vote
2
Answered by 9 years ago

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 --i is where the value is in the table, v is the value itself. Since we're going through a table of children from the workspace, all the values will be instances.
    if v:IsA("BasePart") and v.Name == "Red" then --Since v is a direct reference to each child in the workspace, we can use an if statement to check if it's a part and if it's name is "Red".
        v.BrickColor = BrickColor.new("Bright red") --We can now change the BrickColor to Bright red here.
    end
end

The code above can also be expressed with a numeric for loop:

local children = workspace:GetChildren() --We must make a variable here, unlike in the generic for loop code.
for i = 1,#children do --i is the number of times the script has looped between 1 and the amount of children in workspace.
    if children[i]:IsA("BasePart") and children[i].Name == "Red" then --See how we have to use children[i] to reference the part, unlike in the generic for loop where we can just reference the v variable.
        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) --See how there's no closing parenthesis? The closing parenthesis will be used when the function ends.
    print(hit.Name) --Print the part that hits the other Part.
end) --Here's our closing parenthesis!

function onTouch(hit) --hit is a variable for the argument returned by the Touched event.
    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) --a is a variable for one number, b is a variable for another number.
    return a*b --Let's multiply a by b and return the value of the multiplication.
end

print(multiply(2,5)) --We can now fire the function. a now has a value of 2 and b has a value of five. You should get 10 printed in the output.

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.

0
You should include how 'hit' for Touched events is a built in parameter that Roblox sets. good answer though. Perci1 4988 — 9y
0
Alright, thanks for the tip! Spongocardo 1991 — 9y
0
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! RadiantSolarium 35 — 9y
0
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. RadiantSolarium 35 — 9y
View all comments (2 more)
0
(I meant you could send me a party invite on ROBLOX) RadiantSolarium 35 — 9y
0
NICE ANSWER koolkid8099 705 — 8y
Ad
Log in to vote
1
Answered by
iSvenDerp 233 Moderation Voter
9 years ago

First off the function part of he on touched script makes it a function so u can make a custom function or it makes the function it self run.

We will look at loops

for i = 1, 10 do -- this is saying do this 1 time for how et many times u want in this case 10
    script.Parent.Transparency = i/10 -- say u had a long code to shorten it use pairs
    wait(0.1) -- this is how Long to wait between the loops
end

Find first child, GetChildren, Wait for child First off we will look at find first child

game:FindFirstChild() -- so this is finding the first child of the game. The first child of the game is workspace so if we wanted to do a script on workspace we could do this since there's so many options. 

Next we will look at get children Say u need all the limbs of a player you could use a get children if u wanted to cause that's defining every child. Wait for child is like what the name says it's simply we waiting for the child.

Hope this helped I'm on mobile if it's confusing.:) let me know if I did good.

Log in to vote
0
Answered by
Relatch 550 Moderation Voter
9 years ago

Say you're making a fighting game. Also, say you placed the sword in ServerStorage. In this scenario, you would want to use FindFirstChild or WaitForChildto find the sword.

local sword = game.ServerStorage:FindFirstChild("Sword")

or

local sword = game.ServerStorage:WaitForChild("Sword")

This basically just searches in ServerStorage for the child "Sword". If it finds it, you can do what you want. Otherwise, it will return nil which means in most cases it will give an error.

Next, we have for loops. Basically, you can use this for GetChildren as you wanted to know in the first question. But, that's not it's only reason. You can use this to search through things, which is it's meaning in most cases. Say you're trying to search through a players backpack to see if they have a sword. You would use for i, v in pairs for this.

--kept this simple (only works in a local script)

local player = game.Players.LocalPlayer
local backpack = player.Backpack

for i, v in pairs(backpack) do
    if backpack:FindFirstChild("Sword") then
        print('sword found!')
    end
end

As you can see, this will search through the player's backpack, checking if they have "Sword" in it.

Lastly, we have connections and functions. Basically, the end of this just means it creates a function when a player touches the part in workspace. Then we have the "hit" part, which means it calls the function hit when it is created by the part being touched. Sadly, I cannot remember the name of "hit" right now. :P

--kept this simple as well

workspace.Part.Touched:connect(function(hit)
    hit.Parent.Humanoid.Health = 0
end)

Hopefully this was helpful to you, and sorry if I kept the for loop a little short. I'm doing a little more research on it myself. Accept this as your answer if it helped!

Answer this question