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

How do you find all children that are named a certain part In workspace?

So Im creating clones of a part named BallGreen. They appear in workspace Every time I press the button, so that's all fine and dandy but my problem is when they touch one of my parts named capture it runs my print function once until that specific part gets destroyed by the void. It appears my problem is that I am only running my function addPoints when only one child of workspace which is the clone is touching the part capture. Is there anyway I can run my function when any block named GreenBall touches the part capture?

My Current code

captured = game.Workspace.Model.capture

local debounce = false


function addPoints(collected)
	if collected == game.Workspace.GreenBall then
		if debounce == false then
			debounce = true
			print("totalPoints")
			wait()
			debounce = false
		end
	end
end
captured.Touched:Connect(addPoints)

1 answer

✓ Accepted answer

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) --Wait(n) controls how fast you want to add/subtract.
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

0

can you describe how the scripts works I would like to understand so I can branch off of it incase I need it later

0

Sure, will edit it.

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