you can use a for loop to get the children of the workspace and use an if statement to check its name.
Example:
for i,v in pairs (workspace:GetChildren()) do
if v.Name == "GreenBall" then
print("hurray!")
end
end
Explanation:
To understand this a bit more, I will try to explain this as well as I can.
So, if you don't know what a for loop is, it can be used in different ways and in different situations, for now, I'll just explain what is going on in this current situation then I will elaborate some more on how you can use it in different ways for the future.
Basically, we are using two variables, i(for index) and v(for variable) to loop through what is in the pair. Alright, so we are looping through the children of workspace, so if we add a print(v) it will print every child of workspace.
Now since you are simply trying to get a specific child out of the workspace we use an if statement to check if its name is what we are looking for, in this case, we are looking for GreenBall
a for loop can also be used in different ways, such as looping through a table. How? Well here you go:
local MyTable = {"Orange", "Apple", "Pie"}
for i,v in pairs(MyTable) do
print(v)
end
also, you can use it to count up or down to a number you would like, if you are wondering how you could make use of that then let me give you an example, let's say we want to make a nice fading effect for a parts transparency.
for i = 0, 1, 0.1 do
part.Transparency = i
wait(1)
end
Ok so let's explain how that works, basically, we have the starting value(The first number) which we started from, the ending value(The middle number) which we want to end up with, and the count(Final number) which adds or subtracts the amount we want. We started at 0 transparency, we wanted to end up with an invisible part(1 transparency) and we are adding by 0.1 per second.
Hopefully, you found this somewhat helpful :P
RELATED ARTICLES: pairs and ipairs, Loops
2 comments
can you describe how the scripts works I would like to understand so I can branch off of it incase I need it later
Sure, will edit it.