1
votes

I'm pretty new to ROBLOX development and don't really know how to make the objects talk just yet.

I have 16 cells all with an individual open and close button. Those work. I want to create a button that will open all doors at once.

function onClicked()
    script.Parent.Parent.Door.Transparency = 1
    script.Parent.Parent.Door.CanCollide= false 
end

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

The above function is on each cell and works. I would like to loop through each one and fire it when I click a different button. I've been looking into getting each object with the same names but haven't been able to iterate through it.

The below code is my attempt to get it to fire off each one!

local part = workspace.OpenButton
local clickDetector = Instance.new("ClickDetector")

local function onMouseClick(player)
    for _, child in pairs(workspace.PrisonCell:GetChildren()) do
        print(child)
        child:connect(child.Door.onClicked)
    end
end

clickDetector.Parent = part
part.Parent = workspace

clickDetector.MouseClick:connect(onMouseClick)

enter image description here enter image description here

Any help with this would be greatly appreciated!

1

1 Answers

0
votes

You could do something similar to this to open all of the prison doors at once:

local part = workspace.OpenButton
local clickDetector = Instance.new("ClickDetector")

local function onMouseClick(player)
  for _, child in pairs(workspace.PrisonCell:GetChildren()) do
    child.Door.Transparency = 1
    child.Door.CanCollide = false 
   end
end

clickDetector.Parent = part
part.Parent = workspace

clickDetector.MouseClick:connect(onMouseClick)

The downside to using the solution above is that if you want to change the door opening script, you would have to change it in both the individual cell buttons as well as this master button script. If you think this might be a problem in the future, I would consider writing a master script somewhere in ServerScriptService that uses BindableEvents for communcation with the button clicking scripts in order to keep the door opening function in one place.