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

Get all parts with the same name?

Hi all! I was wondering how you would get the name of a part in the workspace that has the same name? For example if there are lots of parts called "Blue" and they had lights in, how would I "Find" them all and "access" the lights?

I tried FindFirstChild and GetChildren, but it didn't work :/ Thanks!

1 comment

0

You would want to use a for loop gathering all children of a model. Have an if statement that if the Part's name equals blue, then to change the light object. Either that or place the object values into another table and go through that. Without an attempt present, we can not help.

3 answers

I take more Direct approaches so I'll give you an Example code:

local Parts = game.Workspace:GetChildren() for i = 1, #Parts do if Parts[i].Name == "Blue" then Light = Parts[i].Light -- Insert coding for the Lights else return end end


Basically what I did was GetChildren() in Workspace then check for Objects Named "Blue" then when the script gathered all the Objects named "Blue" it would go into each one and find the Child, "Light".... I hope that made sense. I'm trying to get use to Explaining this. xD


function scanInWorkspace(placeToScan)
	for _,object in pairs(placeToScan:GetChildren()) do
		scanInWorkspace(object)
		if object:IsA("BasePart") and object.Name == "Blue" then
			local light = object:FindFirstChild "PointLight"
			if light then
				-- Code
			end
		end
	end
end

scanInWorkspace(workspace)

This function will go through all the children of "placeToScan." The function runs itself again on each child, therefore checking through the whole hierarchy. If the object meets the criteria, it will run code.

You could just change the names of both of them lol

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