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

How do I control multiple models of SAME NAME with script?

Hello everyone. I have a script t activate multiple emergency lights upon pressing a button. The problem is that only one turns on. It toggles the on and off, which I was surprised I could write in one try, but the other several lights (more will exist in the future) are not doing anything. Here is the directory:

[SCRIPT]>>Button>>Controls>>EmergencyLights<<Emergency Light(MULTIPLE)

Here is the script:

function onClicked() if script.Parent.Parent.Parent.EmergencyLight.Toggle.Value == true then script.Parent.Parent.Parent.EmergencyLight.Toggle.Value = false else script.Parent.Parent.Parent.EmergencyLight.Toggle.Value = true end end

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

The script has three parents, the first is the part(button), the second is controls(Group of parts that make up a panel that controls other stuff), and the third is the main group, which contains the lights and controls. The object called "Toggle" is a true/false value that decides whether the light(s) is on. If anyone knows how to tell my script to toggle all of the lights please tell me!

  • PJB

3 comments

0

I really wish that this website recognized the line breaks...

0

It does. Use code blocks.  Edit your post to use a code block.

0

If you want breaks between other text, use a double line break in the text area. This is standard markdown format.

1 answer

✓ Accepted answer
function onClicked() 
if script.Parent.Parent.Parent.EmergencyLight.Toggle.Value == true then script.Parent.Parent.Parent.EmergencyLight.Toggle.Value = false 
else
script.Parent.Parent.Parent.EmergencyLight.Toggle.Value = true 
end 
end
script.Parent.ClickDetector.MouseClick:connect(onClicked)

to turn them all on:

Local LightModel = script.Parent.Parent.Parent
function onClicked() 
if script.Parent.Parent.Parent.EmergencyLight.Toggle.Value == true then 
for k,v in pairs(LightModel:GetChildren()) do
v.Toggle.Value = false
end
else
for k,v in pairs(LightModel:GetChildren()) do
v.Toggle.Value = true
end
end 
end
script.Parent.ClickDetector.MouseClick:connect(onClicked)

should be right, but you might want to change the paths. Essentially, it iterates through the model's parts and changes the value you want.

4 comments

0

Thank you, but now none are turning on. I'll try to change the paths

0

Okay I got it to work. the "Local" at the very top needed to have the L lowercased... LOL but yeah now they all turn on you are genius

0

Whatever you are defining "On" as, either a light being enabled or a material changing to Neon, that what needs to be in place of v.Toggle.Value = true

0

Ey! No probs m80 :)

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