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

How to target multiple objects in your script?

Let's say I want to make a light switch that turns on and off multiple lights. I know how to make it with one light source, but not multiple. All the light sources have parents with the same name and are in the same group. How do I do it?..

Here's the simple script of the light switch:

light = game.Workspace.light2.light.PointLight
function onclick()
	light.Enabled = not light.Enabled
	script.Parent.Click1:play()
	wait(0.1)
	script.Parent.Click2:play()
end

script.Parent.ClickDetector.MouseClick:connect(onclick)

"light2"is the model and "light" is one of the multiple parts of the model. It only turns on/off one of the PointLights.

I'm really sorry for my stupidity, but I couldn't find the answer anywhere.

2 comments

0

To accept answers, click the accept button.

1 answer

✓ Accepted answer

Use a generic for loop to loop through the model and turn on all PointLights.

local model = game.Workspace:WaitForChild("light2")
function onclick()
	script.Parent.Click1:Play()
	wait(0.1)
	script.Parent.Click2:Play()
	for _, part in pairs(model:GetChildren()) do
		local light = part:FindFirstChild("PointLight")
		if light then
			light.Enabled = not light.Enabled
		end
	end
end

script.Parent:WaitForChild("ClickDetector").MouseClick:Connect(onclick)

I also added a few WaitForChild's. Let me know if this works for you, and if you have any questions feel free to ask.

Good Luck!

If I helped, please don't forget to accept my answer.

5 comments

0

Thank you! It works like a charm! I wish I could upvote your answer, but I don't have enough reputation points...

0

I'll Upvote it when I can, I promise! :)

0

;~;

0

I didn't have that button when you posted your answer... That's odd... Believe me, I would've saw it.

0

np. And thanks!

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