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 think maybe you should add the number though a NumberValue, instead of adding it through the script:
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.
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