Script won't work (i cant explain what the script is)

Hello. I was making a script for my game where you have to press/click a certain ClickDetectors before something happens, like part being invisible. When I tried to script what I wanted it to do, it doesn’t work.
Script:

local button1 = script.Parent.a1
local button2 = script.Parent.a2
local button3 = script.Parent.a3
local button4 = script.Parent.a4
local button5 = script.Parent.a5
local wall = script.Parent.Parent.Part
local number = 0

button1.ClickDetector.MouseClick:Connect(function()
	number = number + 1
button2.ClickDetector.MouseClick:Connect(function()
	number = number + 1
button3.ClickDetector.MouseClick:Connect(function()
	number = number + 1
button4.ClickDetector.MouseClick:Connect(function()
	number = number + 1
button2.ClickDetector.MouseClick:Connect(function()
	number = number + 1
	if number == 5 then
		wall.Transparency = 1
		wall.CanCollide = false				
	end
end)))))

I feel like a function inside if a function inside of a function, etc. is a bit unnecessary for this.

There are lots of code door tutorials on youtube, if that is what you are trying to make.

1 Like
  1. I think maybe you should add the number though a NumberValue, instead of adding it through the script:
  1. It would be better to do it as one function, as you do not need to click on all the buttons for the if statement to happen anyways. You could click on the first button until it adds up to five and not have to click any of the rest of them.
  • Otherwise, adding up numbers might not be the best way for this.

I’m not making a code door, I’m making a door that you have to click 5 buttons to open.

local wall = script.Parent.Parent.Part
local number = 0
local buttonClicks = {} -- Save the RBXScriptConnections

for i = 1, 5 do
   -- Use a numerical loop instead of declaring variables as (a1 - 5)
   local button = script.Parent:FindFirstChild(string.format("a%d", i)) -- Find the object from the loop index
   table.insert(buttonClicks, button.ClickDetector.MouseClick:Connect(function()
       -- I'm not sure if you want each button to be clickable more than once
       if number >= 5 then -- If 5 clicks or more
           for _, connection in buttonClicks do
               connection:Disconnect() -- Disconnect the connections
           end
           table.clear(buttonClicks) -- Clear the connections

           wall.Transparency = 1
           wall.CanCollide = false
           return -- Stop the thread
       end

       number += 1 -- Increment the clicks by 1
   end))
end
2 Likes

Still did not work.
Do I have to put the buttons in a model or something?

What went wrong? Did it error?

No, it didn’t show a error in the output.

Is this a LocalScript? If so, change it to a server script


Yes

It’s a server script, not a LocalScript.
I can show you the model in the explorer if you want.

image

Here’s a fixed version:

local button1 = script.Parent.a1
local button2 = script.Parent.a2
local button3 = script.Parent.a3
local button4 = script.Parent.a4
local button5 = script.Parent.a5
local wall = script.Parent.wall

button1.ClickDetector.MouseClick:Connect(function()
	button2.ClickDetector.MouseClick:Connect(function()
		button3.ClickDetector.MouseClick:Connect(function()
			button4.ClickDetector.MouseClick:Connect(function()
				button2.ClickDetector.MouseClick:Connect(function()
					wall.Transparency = 1
					wall.CanCollide = false	
				end)
			end)
		end)
	end)
end)

You also don’t need the numbers since it’s always conditional and relies on the previous buttons being executed.

1 Like

Try using either my updated post or @ATC4412’s solution