My code is not working as intended (more info in description)

I am making a pattern type puzzle for a game I’m working on. The way it works is there are four symbols. The pattern will quickly flash the order of these symbols. The player must then click on each of them in that order, creating the same pattern. This will of course reward the player (the reward is irrelevant.) If the player messes something up, a “trigger” function will be called, (what the trigger function does is also irrelevant.) All of this works fine the first time the pattern is activated. But the second time it get’s activated, it calls the “trigger” function when it shouldn’t. Same goes for every time the player attempts to complete the puzzle afterwards. This is of course a major issue, since the player is being wrongfully penalized. Any help would be much appreciated!

local stage = 1
local debounce = false
local tweenservice = game:GetService("TweenService")

local circle = script.Parent.Circle
local button1 = script.Parent.One.Button1
local button2 = script.Parent.Two.Button2
local button3 = script.Parent.Three.Button3
local button4 = script.Parent.Four.Button4

local pattern1 = {button1,button2,button3, button4}
local pattern2 = {button3,button1,button4, button2}
local pattern3 = {button4,button3,button1, button2}
local pattern4 = {button2,button4,button3, button1}

local patternsTable = {pattern1, pattern2, pattern3, pattern4}

script.Parent.Trigger.Touched:Connect(function()
	if debounce == false then
		debounce = true
		--Activate buttons
		wait(0.5)
		circle.Transparency = 0
		wait(0.5)
		circle.Transparency = 1
		button3.Transparency = 0
		wait(0.5)
		circle.Transparency = 0
		wait(0.5)
		circle.Transparency = 1
		button2.Transparency = 0
		wait(0.5)
		circle.Transparency = 0
		wait(0.5)
		circle.Transparency = 1
		button1.Transparency = 0
		wait(0.5)
		circle.Transparency = 0
		wait(0.5)
		circle.Transparency = 1
		button4.Transparency = 0
		wait(0.5)
		circle.Transparency = 1
		
		local selectedPattern = patternsTable[math.random(1, #patternsTable)]
		
		selectedPattern[1].BrickColor = BrickColor.new("Medium blue")
		wait(0.5)
		selectedPattern[1].BrickColor = BrickColor.new("202, 203, 209")
		
		selectedPattern[2].BrickColor = BrickColor.new("Medium blue")
		wait(0.5)
		selectedPattern[2].BrickColor = BrickColor.new("202, 203, 209")
		
		selectedPattern[3].BrickColor = BrickColor.new("Medium blue")
		wait(0.5)
		selectedPattern[3].BrickColor = BrickColor.new("202, 203, 209")
		
		selectedPattern[4].BrickColor = BrickColor.new("Medium blue")
		wait(0.5)
		selectedPattern[4].BrickColor = BrickColor.new("202, 203, 209")
		
		wait(0.5)
		
		selectedPattern[1].Parent.Button.ClickDetector.MaxActivationDistance = 15
		selectedPattern[2].Parent.Button.ClickDetector.MaxActivationDistance = 15
		selectedPattern[3].Parent.Button.ClickDetector.MaxActivationDistance = 15
		selectedPattern[4].Parent.Button.ClickDetector.MaxActivationDistance = 15
		
		
		
		selectedPattern[1].Parent.Button.ClickDetector.MouseClick:Connect(function()
			if stage == 1 then
				print("Stage one complete")
				stage = 2
				selectedPattern[1].BrickColor = BrickColor.new("Sand green")
				selectedPattern[1].Parent.Button.ClickDetector.MaxActivationDistance = 0
			else
				trigger()
			end
		end)
		
		selectedPattern[2].Parent.Button.ClickDetector.MouseClick:Connect(function()
			if stage == 2 then
				print("Stage two complete")
				stage = 3
				selectedPattern[2].BrickColor = BrickColor.new("Sand green")
				selectedPattern[2].Parent.Button.ClickDetector.MaxActivationDistance = 0
			else
				trigger()
			end
		end)
		
		selectedPattern[3].Parent.Button.ClickDetector.MouseClick:Connect(function()
			if stage == 3 then
				print("Stage three complete")
				stage = 4
				selectedPattern[3].BrickColor = BrickColor.new("Sand green")
				selectedPattern[3].Parent.Button.ClickDetector.MaxActivationDistance = 0
			else
				trigger()
			end
		end)
		
		selectedPattern[4].Parent.Button.ClickDetector.MouseClick:Connect(function()
			if stage == 4 then
				print("Stage four complete")
				print("You got the pattern correct!")
				stage = 1
				selectedPattern[4].BrickColor = BrickColor.new("Sand green")
				selectedPattern[4].Parent.Button.ClickDetector.MaxActivationDistance = 0
				
				wait(0.5)
				
				selectedPattern[1].BrickColor = BrickColor.new("Institutional white")
				selectedPattern[2].BrickColor = BrickColor.new("Institutional white")
				selectedPattern[3].BrickColor = BrickColor.new("Institutional white")
				selectedPattern[4].BrickColor = BrickColor.new("Institutional white")
				circle.BrickColor = BrickColor.new("Institutional white")
				wait(0.3)
				selectedPattern[1].BrickColor = BrickColor.new("Sand green")
				selectedPattern[2].BrickColor = BrickColor.new("Sand green")
				selectedPattern[3].BrickColor = BrickColor.new("Sand green")
				selectedPattern[4].BrickColor = BrickColor.new("Sand green")
				circle.BrickColor = BrickColor.new("Sand green")
				wait(0.3)
				selectedPattern[1].BrickColor = BrickColor.new("Institutional white")
				selectedPattern[2].BrickColor = BrickColor.new("Institutional white")
				selectedPattern[3].BrickColor = BrickColor.new("Institutional white")
				selectedPattern[4].BrickColor = BrickColor.new("Institutional white")
				circle.BrickColor = BrickColor.new("Institutional white")
				wait(0.3)
				selectedPattern[1].BrickColor = BrickColor.new("Sand green")
				selectedPattern[2].BrickColor = BrickColor.new("Sand green")
				selectedPattern[3].BrickColor = BrickColor.new("Sand green")
				selectedPattern[4].BrickColor = BrickColor.new("Sand green")
				circle.BrickColor = BrickColor.new("Sand green")
				wait(0.3)
				selectedPattern[1].BrickColor = BrickColor.new("Institutional white")
				selectedPattern[2].BrickColor = BrickColor.new("Institutional white")
				selectedPattern[3].BrickColor = BrickColor.new("Institutional white")
				selectedPattern[4].BrickColor = BrickColor.new("Institutional white")
				circle.BrickColor = BrickColor.new("Institutional white")
				wait(0.3)
				selectedPattern[1].BrickColor = BrickColor.new("Sand green")
				selectedPattern[2].BrickColor = BrickColor.new("Sand green")
				selectedPattern[3].BrickColor = BrickColor.new("Sand green")
				selectedPattern[4].BrickColor = BrickColor.new("Sand green")
				circle.BrickColor = BrickColor.new("Sand green")
				wait(0.3)
				selectedPattern[1].BrickColor = BrickColor.new("Institutional white")
				selectedPattern[2].BrickColor = BrickColor.new("Institutional white")
				selectedPattern[3].BrickColor = BrickColor.new("Institutional white")
				selectedPattern[4].BrickColor = BrickColor.new("Institutional white")
				circle.BrickColor = BrickColor.new("Institutional white")
				wait(0.3)
				selectedPattern[1].BrickColor = BrickColor.new("Sand green")
				selectedPattern[2].BrickColor = BrickColor.new("Sand green")
				selectedPattern[3].BrickColor = BrickColor.new("Sand green")
				selectedPattern[4].BrickColor = BrickColor.new("Sand green")
				circle.BrickColor = BrickColor.new("Sand green")
				
				wait(0.5)
				circle.Transparency = 1
				wait(0.5)
				circle.Transparency = 0
				button3.Transparency = 1
				wait(0.5)
				circle.Transparency = 1
				wait(0.5)
				circle.Transparency = 0
				button2.Transparency = 1
				wait(0.5)
				circle.Transparency = 1
				wait(0.5)
				circle.Transparency = 0
				button1.Transparency = 1
				wait(0.5)
				circle.Transparency = 1
				wait(0.5)
				circle.Transparency = 0
				button4.Transparency = 1
				wait(0.5)
				circle.Transparency = 1
				
				wait(1)
				
				tweenservice:Create(script.Parent.Door, TweenInfo.new(5, Enum.EasingStyle.Quad, Enum.EasingDirection.InOut), {Position = script.Parent.Door.Position + Vector3.new(0,7,0)}):Play()
						tweenservice:Create(script.Parent.Screen, TweenInfo.new(5, Enum.EasingStyle.Quad, Enum.EasingDirection.InOut), {Position = script.Parent.Screen.Position + Vector3.new(0,7,0)}):Play()
						wait(10)
						tweenservice:Create(script.Parent.Door, TweenInfo.new(5, Enum.EasingStyle.Quad, Enum.EasingDirection.InOut), {Position = script.Parent.Door.Position + Vector3.new(0,-7,0)}):Play()
						tweenservice:Create(script.Parent.Screen, TweenInfo.new(5, Enum.EasingStyle.Quad, Enum.EasingDirection.InOut), {Position = script.Parent.Screen.Position + Vector3.new(0,-7,0)}):Play()
				wait(6)
				selectedPattern[1].BrickColor = BrickColor.new("Institutional white")
				selectedPattern[2].BrickColor = BrickColor.new("Institutional white")
				selectedPattern[3].BrickColor = BrickColor.new("Institutional white")
				selectedPattern[4].BrickColor = BrickColor.new("Institutional white")
				circle.BrickColor = BrickColor.new("Institutional white")
				stage = 1
				debounce = false
				print("End of script")
				
			else
				trigger()
			end
		end)
		
		
		
		
		
		
	end
end)



function trigger()
	print("Triggered (You got the pattern incorrect)")
end

Attempt Num. 1 Output:

23:34:55.321 Stage one complete - Server - Script:204
23:34:56.037 Stage two complete - Server - Script:215
23:34:56.654 Stage three complete - Server - Script:226
23:34:57.237 Stage four complete - Server - Script:237
23:34:57.237 You got the pattern correct! - Server - Script:238

Attempt Num. 2 Output:

Stage one complete - Server - Script:204
23:36:03.551 Stage two complete - Server - Script:215
23:36:04.317 Triggered (You got the pattern incorrect) - Server - Script:349
23:36:04.318 Stage three complete - Server - Script:226
23:36:04.884 :arrow_forward: Triggered (You got the pattern incorrect) (x2) - Server - Script:349
23:36:06.134 Stage four complete - Server - Script:237
23:36:06.135 You got the pattern correct! - Server - Script:238
23:36:06.135 Triggered (You got the pattern incorrect) - Server - Script:349

(Pressing the first button causes the first two print statements to occur simultaneously in the second output. This is of course, not supposed to happen.

I am aware that the code is horrendously written and not organized at all. Most of it doesn’t contribute to functionality.

After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

-- This is an example Lua code block

Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.

2 Likes

When an attempt is failed and the if then else statement is called the stage number is never reset to one. As it is only changed when the script first runs and after successful attempts to numbers greater than one. Therefore it never meets any conditions and automatically calls trigger() again.

1 Like

Yes, that is correct. The issue is that the triggers occur even when you never actually press a button incorrectly. it should never be triggering in the first place.

1 Like

The debounce is also never reset on any failed attempts, only on successful ones.

2 Likes

True, but again, it doesn’t matter because all the tests I’ve run, I have done the pattern correctly. It should not, under any circumstances, trigger.

1 Like

Try to make the script also print the current stage and whether the debounce is true or false. You may have some sort of conflict causing multiple outcomes or multiple functions.

1 Like

I tried that, still no clear indicator of what the issue is

1 Like

hmm okay, have you attempted a debounce that turns on and off as each stage is running?

1 Like

I don’t really know how doing that would be helpful. Notice how I have the following code:

if stage == 1 then
and
if stage == 2 then

Yet, when I play, both of these run simultaneously. This should be impossible?? How can the stage variable equal two thing at once?

If the function is being fired multiple times it could be contradicting itself.

The debounce prevents that. The function will not run until it is already complete

okay, could you add a specific print statement per stage that fires alongside trigger, this could help narrow down which function is causing the issue.

There already are print statements for each. Check the output that I sent above

because you never disconnect the previous events and every time the touched event is triggered it creates a new event that overlaps the previous ones
this should fix it

local stage = 1
local debounce = false
local tweenservice = game:GetService("TweenService")

local circle = script.Parent.Circle
local button1 = script.Parent.One.Button1
local button2 = script.Parent.Two.Button2
local button3 = script.Parent.Three.Button3
local button4 = script.Parent.Four.Button4

local pattern1 = {button1,button2,button3, button4}
local pattern2 = {button3,button1,button4, button2}
local pattern3 = {button4,button3,button1, button2}
local pattern4 = {button2,button4,button3, button1}

local patternsTable = {pattern1, pattern2, pattern3, pattern4}

script.Parent.Trigger.Touched:Connect(function()
	if debounce == false then
		debounce = true
		--Activate buttons
		wait(0.5)
		circle.Transparency = 0
		wait(0.5)
		circle.Transparency = 1
		button3.Transparency = 0
		wait(0.5)
		circle.Transparency = 0
		wait(0.5)
		circle.Transparency = 1
		button2.Transparency = 0
		wait(0.5)
		circle.Transparency = 0
		wait(0.5)
		circle.Transparency = 1
		button1.Transparency = 0
		wait(0.5)
		circle.Transparency = 0
		wait(0.5)
		circle.Transparency = 1
		button4.Transparency = 0
		wait(0.5)
		circle.Transparency = 1

		local selectedPattern = patternsTable[math.random(1, #patternsTable)]

		selectedPattern[1].BrickColor = BrickColor.new("Medium blue")
		wait(0.5)
		selectedPattern[1].BrickColor = BrickColor.new("202, 203, 209")

		selectedPattern[2].BrickColor = BrickColor.new("Medium blue")
		wait(0.5)
		selectedPattern[2].BrickColor = BrickColor.new("202, 203, 209")

		selectedPattern[3].BrickColor = BrickColor.new("Medium blue")
		wait(0.5)
		selectedPattern[3].BrickColor = BrickColor.new("202, 203, 209")

		selectedPattern[4].BrickColor = BrickColor.new("Medium blue")
		wait(0.5)
		selectedPattern[4].BrickColor = BrickColor.new("202, 203, 209")

		wait(0.5)

		selectedPattern[1].Parent.Button.ClickDetector.MaxActivationDistance = 15
		selectedPattern[2].Parent.Button.ClickDetector.MaxActivationDistance = 15
		selectedPattern[3].Parent.Button.ClickDetector.MaxActivationDistance = 15
		selectedPattern[4].Parent.Button.ClickDetector.MaxActivationDistance = 15

		local AllConnections = {}
		local Pattern1Connection
		Pattern1Connection = selectedPattern[1].Parent.Button.ClickDetector.MouseClick:Connect(function()
			if stage == 1 then
				print("Stage one complete")
				stage = 2
				selectedPattern[1].BrickColor = BrickColor.new("Sand green")
				selectedPattern[1].Parent.Button.ClickDetector.MaxActivationDistance = 0
			else
				trigger()
			end
		end)
		table.insert(AllConnections,Pattern1Connection)
		
		local Pattern2Connection
		Pattern2Connection = selectedPattern[2].Parent.Button.ClickDetector.MouseClick:Connect(function()
			if stage == 2 then
				print("Stage two complete")
				stage = 3
				selectedPattern[2].BrickColor = BrickColor.new("Sand green")
				selectedPattern[2].Parent.Button.ClickDetector.MaxActivationDistance = 0
			else
				trigger()
			end
		end)
		table.insert(AllConnections,Pattern2Connection)
		local Pattern3Connection
		Pattern3Connection = selectedPattern[3].Parent.Button.ClickDetector.MouseClick:Connect(function()
			if stage == 3 then
				print("Stage three complete")
				stage = 4
				selectedPattern[3].BrickColor = BrickColor.new("Sand green")
				selectedPattern[3].Parent.Button.ClickDetector.MaxActivationDistance = 0
			else
				trigger()
			end
		end)
		table.insert(AllConnections,Pattern3Connection)
		local Pattern4Connection
		Pattern4Connection = selectedPattern[4].Parent.Button.ClickDetector.MouseClick:Connect(function()
			if stage == 4 then
				print("Stage four complete")
				print("You got the pattern correct!")
				stage = 1
				selectedPattern[4].BrickColor = BrickColor.new("Sand green")
				selectedPattern[4].Parent.Button.ClickDetector.MaxActivationDistance = 0

				wait(0.5)

				selectedPattern[1].BrickColor = BrickColor.new("Institutional white")
				selectedPattern[2].BrickColor = BrickColor.new("Institutional white")
				selectedPattern[3].BrickColor = BrickColor.new("Institutional white")
				selectedPattern[4].BrickColor = BrickColor.new("Institutional white")
				circle.BrickColor = BrickColor.new("Institutional white")
				wait(0.3)
				selectedPattern[1].BrickColor = BrickColor.new("Sand green")
				selectedPattern[2].BrickColor = BrickColor.new("Sand green")
				selectedPattern[3].BrickColor = BrickColor.new("Sand green")
				selectedPattern[4].BrickColor = BrickColor.new("Sand green")
				circle.BrickColor = BrickColor.new("Sand green")
				wait(0.3)
				selectedPattern[1].BrickColor = BrickColor.new("Institutional white")
				selectedPattern[2].BrickColor = BrickColor.new("Institutional white")
				selectedPattern[3].BrickColor = BrickColor.new("Institutional white")
				selectedPattern[4].BrickColor = BrickColor.new("Institutional white")
				circle.BrickColor = BrickColor.new("Institutional white")
				wait(0.3)
				selectedPattern[1].BrickColor = BrickColor.new("Sand green")
				selectedPattern[2].BrickColor = BrickColor.new("Sand green")
				selectedPattern[3].BrickColor = BrickColor.new("Sand green")
				selectedPattern[4].BrickColor = BrickColor.new("Sand green")
				circle.BrickColor = BrickColor.new("Sand green")
				wait(0.3)
				selectedPattern[1].BrickColor = BrickColor.new("Institutional white")
				selectedPattern[2].BrickColor = BrickColor.new("Institutional white")
				selectedPattern[3].BrickColor = BrickColor.new("Institutional white")
				selectedPattern[4].BrickColor = BrickColor.new("Institutional white")
				circle.BrickColor = BrickColor.new("Institutional white")
				wait(0.3)
				selectedPattern[1].BrickColor = BrickColor.new("Sand green")
				selectedPattern[2].BrickColor = BrickColor.new("Sand green")
				selectedPattern[3].BrickColor = BrickColor.new("Sand green")
				selectedPattern[4].BrickColor = BrickColor.new("Sand green")
				circle.BrickColor = BrickColor.new("Sand green")
				wait(0.3)
				selectedPattern[1].BrickColor = BrickColor.new("Institutional white")
				selectedPattern[2].BrickColor = BrickColor.new("Institutional white")
				selectedPattern[3].BrickColor = BrickColor.new("Institutional white")
				selectedPattern[4].BrickColor = BrickColor.new("Institutional white")
				circle.BrickColor = BrickColor.new("Institutional white")
				wait(0.3)
				selectedPattern[1].BrickColor = BrickColor.new("Sand green")
				selectedPattern[2].BrickColor = BrickColor.new("Sand green")
				selectedPattern[3].BrickColor = BrickColor.new("Sand green")
				selectedPattern[4].BrickColor = BrickColor.new("Sand green")
				circle.BrickColor = BrickColor.new("Sand green")

				wait(0.5)
				circle.Transparency = 1
				wait(0.5)
				circle.Transparency = 0
				button3.Transparency = 1
				wait(0.5)
				circle.Transparency = 1
				wait(0.5)
				circle.Transparency = 0
				button2.Transparency = 1
				wait(0.5)
				circle.Transparency = 1
				wait(0.5)
				circle.Transparency = 0
				button1.Transparency = 1
				wait(0.5)
				circle.Transparency = 1
				wait(0.5)
				circle.Transparency = 0
				button4.Transparency = 1
				wait(0.5)
				circle.Transparency = 1

				wait(1)

				tweenservice:Create(script.Parent.Door, TweenInfo.new(5, Enum.EasingStyle.Quad, Enum.EasingDirection.InOut), {Position = script.Parent.Door.Position + Vector3.new(0,7,0)}):Play()
				tweenservice:Create(script.Parent.Screen, TweenInfo.new(5, Enum.EasingStyle.Quad, Enum.EasingDirection.InOut), {Position = script.Parent.Screen.Position + Vector3.new(0,7,0)}):Play()
				wait(10)
				tweenservice:Create(script.Parent.Door, TweenInfo.new(5, Enum.EasingStyle.Quad, Enum.EasingDirection.InOut), {Position = script.Parent.Door.Position + Vector3.new(0,-7,0)}):Play()
				tweenservice:Create(script.Parent.Screen, TweenInfo.new(5, Enum.EasingStyle.Quad, Enum.EasingDirection.InOut), {Position = script.Parent.Screen.Position + Vector3.new(0,-7,0)}):Play()
				wait(6)
				selectedPattern[1].BrickColor = BrickColor.new("Institutional white")
				selectedPattern[2].BrickColor = BrickColor.new("Institutional white")
				selectedPattern[3].BrickColor = BrickColor.new("Institutional white")
				selectedPattern[4].BrickColor = BrickColor.new("Institutional white")
				circle.BrickColor = BrickColor.new("Institutional white")
				stage = 1
				debounce = false
				print("End of script")

			else
				trigger()
			end
			
		end)
		table.insert(AllConnections,Pattern4Connection)
		
		-- Disconnect All Connections
		for i, Connection:RBXScriptConnection in pairs(AllConnections) do
			Connection:Disconnect()
		end





	end
end)



function trigger()
	print("Triggered (You got the pattern incorrect)")
end

please give me an update if it works or not

1 Like

I really recommend using for loops, they can optimize code a LOT and make it easier to read and modify

1 Like

It sadly does not work. I’ve tried everything I can think of. Nothing is working

can you post the entirety of your current script?

Hey, so I recreated the script, utilizing loops and whatnot to make the code more efficient and improve readability. I provided you with an in-depth look at the output as well. The output results will mean nothing to you unless you read through the script first. Here is the new code:

-- // Variables

local debounce = false
local tweenservice = game:GetService("TweenService")

local stage = script.Parent.Stage

local circle = script.Parent.Circle
local button1 = script.Parent.One.Button1
local button2 = script.Parent.Two.Button2
local button3 = script.Parent.Three.Button3
local button4 = script.Parent.Four.Button4

-- // Colors

local blue = BrickColor.new("Medium blue")
local white = BrickColor.new("Institutional white")
local green = BrickColor.new("Sand green")

script.Parent.Trigger.Touched:Connect(function()
	if debounce == false then
		debounce = true
print(stage.Value.. " at the beginning")
		stage.Value = 1
		
		-- // Randomly select a pattern
		

		local pattern1 = {button1,button2,button3, button4}
		local pattern2 = {button3,button1,button4, button2}
		local pattern3 = {button4,button3,button1, button2}
		local pattern4 = {button2,button4,button3, button1}

		local buttons = {button3, button2, button1, button4}

		local patternsTable = {pattern1, pattern2, pattern3, pattern4}

		local selectedPattern = patternsTable[math.random(1, #patternsTable)]
		
		
		
		-- // Irrelevent (just looks cool)

		for i, v in pairs(buttons) do
			wait(0.5)
			circle.Transparency = 0
			wait(0.5)
			circle.Transparency = 1
			v.Transparency = 0
		end

		wait(1)

		for i, v in pairs(selectedPattern) do
			v.BrickColor = blue
			wait(0.5)
			v.BrickColor = white
		end
		
		-- // Activate clickdetectors

		for i, v in pairs(selectedPattern) do
			v.Parent.Button.ClickDetector.MaxActivationDistance = 15
		end

		-- // Primary code

		selectedPattern[1].Parent.Button.ClickDetector.MouseClick:Connect(function()
			if stage.Value == 1 then
				stage.Value = 2
				selectedPattern[1].BrickColor = green
				selectedPattern[1].Parent.Button.ClickDetector.MaxActivationDistance = 0
			else
				print(stage.Value)
				print("Num 1")
				trigger()
			end
		end)

		selectedPattern[2].Parent.Button.ClickDetector.MouseClick:Connect(function()
			if stage.Value == 2 then
				stage.Value = 3
				selectedPattern[2].BrickColor = green
				selectedPattern[2].Parent.Button.ClickDetector.MaxActivationDistance = 0
			else
				print(stage.Value)
				print("Num 2")
				trigger()
			end
		end)

		selectedPattern[3].Parent.Button.ClickDetector.MouseClick:Connect(function()
			if stage.Value == 3 then
				stage.Value = 4
				selectedPattern[3].BrickColor = green
				selectedPattern[3].Parent.Button.ClickDetector.MaxActivationDistance = 0
			else
				print(stage.Value)
				print("Num 3")
				trigger()
			end
		end)

		selectedPattern[4].Parent.Button.ClickDetector.MouseClick:Connect(function()
			if stage.Value == 4 then
				stage.Value = 1
				selectedPattern[4].BrickColor = green
				selectedPattern[4].Parent.Button.ClickDetector.MaxActivationDistance = 0
				
				wait(0.5)
				
				--Not important (Just looks cool)
				
				for i = 1, 3 do --loops 6 times.
					button1.BrickColor = white
					button2.BrickColor = white
					button3.BrickColor = white
					button4.BrickColor = white
					circle.BrickColor = white
					wait(0.3)
					button1.BrickColor = green
					button2.BrickColor = green
					button3.BrickColor = green
					button4.BrickColor = green
					circle.BrickColor = green
					wait(0.3)
				end
				
				wait(0.5)
				
				for i, v in pairs(buttons) do
					wait(0.5)
					circle.Transparency = 1
					wait(0.5)
					circle.Transparency = 0
					v.Transparency = 1
				end
				
				circle.Transparency = 1
				circle.BrickColor = white
				
				-- // Tweening
				
				tweenservice:Create(script.Parent.Door, TweenInfo.new(5, Enum.EasingStyle.Quad, Enum.EasingDirection.InOut), {Position = script.Parent.Door.Position + Vector3.new(0,7,0)}):Play()
				tweenservice:Create(script.Parent.Screen, TweenInfo.new(5, Enum.EasingStyle.Quad, Enum.EasingDirection.InOut), {Position = script.Parent.Screen.Position + Vector3.new(0,7,0)}):Play()
				wait(10)
				tweenservice:Create(script.Parent.Door, TweenInfo.new(5, Enum.EasingStyle.Quad, Enum.EasingDirection.InOut), {Position = script.Parent.Door.Position + Vector3.new(0,-7,0)}):Play()
				tweenservice:Create(script.Parent.Screen, TweenInfo.new(5, Enum.EasingStyle.Quad, Enum.EasingDirection.InOut), {Position = script.Parent.Screen.Position + Vector3.new(0,-7,0)}):Play()
				
				for i, v in pairs(buttons) do
					v.BrickColor = white
				end
				
				wait(10)
				
				-- // Reset values
				
				stage.Value = 1
				selectedPattern = nil
				wait(3)
				debounce = false
				
				print(stage.Value.. " at the end")
				
			else
				print(stage.Value)
				print("Num 4")
				trigger()
			end
		end)


	end
end)

-- // Player messed up

function trigger()
	print("Triggered!")
end

The output:

First time the code runs: (This is exactly how the output should look everytime the code is run)
The value of the number value, ‘stage’ is: 1 at the start of the script.
The value of the number value, ‘stage’ is: 2 at the end of the first button function
The value of the number value, ‘stage’ is: 3 at the end of the second button function
The value of the number value, ‘stage’ is: 4 at the end of the third button function
The value of the number value, ‘stage’ is: 1 at the end of the fourth button function
The value of the number value, ‘stage’ is: 1 at the end of the script.

Second time it runs:

Num 2
Triggered!
3
Num 4
Triggered!
The value of the number value, ‘stage’ is: 4 at the end of the third button function
4
Num 3
Triggered!
The value of the number value, ‘stage’ is: 1 at the end of the fourth button function
Workspace.Map.Sec4CodeDoor.Revised:71: attempt to index nil with number -Line 71
The value of the number value, ‘stage’ is: 1 at the end of the script.

ah no wonder it didn’t work I made a mistake the connections should be disconnected when the 4th Pattern Has been clicked

you can copy and paste using this code or edit your new one:

local stage = 1
local debounce = false
local tweenservice = game:GetService("TweenService")

local circle = script.Parent.Circle
local button1 = script.Parent.One.Button1
local button2 = script.Parent.Two.Button2
local button3 = script.Parent.Three.Button3
local button4 = script.Parent.Four.Button4

local pattern1 = {button1,button2,button3, button4}
local pattern2 = {button3,button1,button4, button2}
local pattern3 = {button4,button3,button1, button2}
local pattern4 = {button2,button4,button3, button1}

local patternsTable = {pattern1, pattern2, pattern3, pattern4}

script.Parent.Trigger.Touched:Connect(function()
	if debounce == false then
		debounce = true
		--Activate buttons
		wait(0.5)
		circle.Transparency = 0
		wait(0.5)
		circle.Transparency = 1
		button3.Transparency = 0
		wait(0.5)
		circle.Transparency = 0
		wait(0.5)
		circle.Transparency = 1
		button2.Transparency = 0
		wait(0.5)
		circle.Transparency = 0
		wait(0.5)
		circle.Transparency = 1
		button1.Transparency = 0
		wait(0.5)
		circle.Transparency = 0
		wait(0.5)
		circle.Transparency = 1
		button4.Transparency = 0
		wait(0.5)
		circle.Transparency = 1

		local selectedPattern = patternsTable[math.random(1, #patternsTable)]

		selectedPattern[1].BrickColor = BrickColor.new("Medium blue")
		wait(0.5)
		selectedPattern[1].BrickColor = BrickColor.new("202, 203, 209")

		selectedPattern[2].BrickColor = BrickColor.new("Medium blue")
		wait(0.5)
		selectedPattern[2].BrickColor = BrickColor.new("202, 203, 209")

		selectedPattern[3].BrickColor = BrickColor.new("Medium blue")
		wait(0.5)
		selectedPattern[3].BrickColor = BrickColor.new("202, 203, 209")

		selectedPattern[4].BrickColor = BrickColor.new("Medium blue")
		wait(0.5)
		selectedPattern[4].BrickColor = BrickColor.new("202, 203, 209")

		wait(0.5)

		selectedPattern[1].Parent.Button.ClickDetector.MaxActivationDistance = 15
		selectedPattern[2].Parent.Button.ClickDetector.MaxActivationDistance = 15
		selectedPattern[3].Parent.Button.ClickDetector.MaxActivationDistance = 15
		selectedPattern[4].Parent.Button.ClickDetector.MaxActivationDistance = 15

		local AllConnections = {}
		local Pattern1Connection
		Pattern1Connection = selectedPattern[1].Parent.Button.ClickDetector.MouseClick:Connect(function()
			if stage == 1 then
				print("Stage one complete")
				stage = 2
				selectedPattern[1].BrickColor = BrickColor.new("Sand green")
				selectedPattern[1].Parent.Button.ClickDetector.MaxActivationDistance = 0
			else
				trigger()
			end
		end)
		table.insert(AllConnections,Pattern1Connection)
		
		local Pattern2Connection
		Pattern2Connection = selectedPattern[2].Parent.Button.ClickDetector.MouseClick:Connect(function()
			if stage == 2 then
				print("Stage two complete")
				stage = 3
				selectedPattern[2].BrickColor = BrickColor.new("Sand green")
				selectedPattern[2].Parent.Button.ClickDetector.MaxActivationDistance = 0
			else
				trigger()
			end
		end)
		table.insert(AllConnections,Pattern2Connection)
		local Pattern3Connection
		Pattern3Connection = selectedPattern[3].Parent.Button.ClickDetector.MouseClick:Connect(function()
			if stage == 3 then
				print("Stage three complete")
				stage = 4
				selectedPattern[3].BrickColor = BrickColor.new("Sand green")
				selectedPattern[3].Parent.Button.ClickDetector.MaxActivationDistance = 0
			else
				trigger()
			end
		end)
		table.insert(AllConnections,Pattern3Connection)
		local Pattern4Connection
		Pattern4Connection = selectedPattern[4].Parent.Button.ClickDetector.MouseClick:Connect(function()
			if stage == 4 then
				print("Stage four complete")
				print("You got the pattern correct!")
				stage = 1
				selectedPattern[4].BrickColor = BrickColor.new("Sand green")
				selectedPattern[4].Parent.Button.ClickDetector.MaxActivationDistance = 0

				wait(0.5)

				selectedPattern[1].BrickColor = BrickColor.new("Institutional white")
				selectedPattern[2].BrickColor = BrickColor.new("Institutional white")
				selectedPattern[3].BrickColor = BrickColor.new("Institutional white")
				selectedPattern[4].BrickColor = BrickColor.new("Institutional white")
				circle.BrickColor = BrickColor.new("Institutional white")
				wait(0.3)
				selectedPattern[1].BrickColor = BrickColor.new("Sand green")
				selectedPattern[2].BrickColor = BrickColor.new("Sand green")
				selectedPattern[3].BrickColor = BrickColor.new("Sand green")
				selectedPattern[4].BrickColor = BrickColor.new("Sand green")
				circle.BrickColor = BrickColor.new("Sand green")
				wait(0.3)
				selectedPattern[1].BrickColor = BrickColor.new("Institutional white")
				selectedPattern[2].BrickColor = BrickColor.new("Institutional white")
				selectedPattern[3].BrickColor = BrickColor.new("Institutional white")
				selectedPattern[4].BrickColor = BrickColor.new("Institutional white")
				circle.BrickColor = BrickColor.new("Institutional white")
				wait(0.3)
				selectedPattern[1].BrickColor = BrickColor.new("Sand green")
				selectedPattern[2].BrickColor = BrickColor.new("Sand green")
				selectedPattern[3].BrickColor = BrickColor.new("Sand green")
				selectedPattern[4].BrickColor = BrickColor.new("Sand green")
				circle.BrickColor = BrickColor.new("Sand green")
				wait(0.3)
				selectedPattern[1].BrickColor = BrickColor.new("Institutional white")
				selectedPattern[2].BrickColor = BrickColor.new("Institutional white")
				selectedPattern[3].BrickColor = BrickColor.new("Institutional white")
				selectedPattern[4].BrickColor = BrickColor.new("Institutional white")
				circle.BrickColor = BrickColor.new("Institutional white")
				wait(0.3)
				selectedPattern[1].BrickColor = BrickColor.new("Sand green")
				selectedPattern[2].BrickColor = BrickColor.new("Sand green")
				selectedPattern[3].BrickColor = BrickColor.new("Sand green")
				selectedPattern[4].BrickColor = BrickColor.new("Sand green")
				circle.BrickColor = BrickColor.new("Sand green")
				wait(0.3)
				selectedPattern[1].BrickColor = BrickColor.new("Institutional white")
				selectedPattern[2].BrickColor = BrickColor.new("Institutional white")
				selectedPattern[3].BrickColor = BrickColor.new("Institutional white")
				selectedPattern[4].BrickColor = BrickColor.new("Institutional white")
				circle.BrickColor = BrickColor.new("Institutional white")
				wait(0.3)
				selectedPattern[1].BrickColor = BrickColor.new("Sand green")
				selectedPattern[2].BrickColor = BrickColor.new("Sand green")
				selectedPattern[3].BrickColor = BrickColor.new("Sand green")
				selectedPattern[4].BrickColor = BrickColor.new("Sand green")
				circle.BrickColor = BrickColor.new("Sand green")

				wait(0.5)
				circle.Transparency = 1
				wait(0.5)
				circle.Transparency = 0
				button3.Transparency = 1
				wait(0.5)
				circle.Transparency = 1
				wait(0.5)
				circle.Transparency = 0
				button2.Transparency = 1
				wait(0.5)
				circle.Transparency = 1
				wait(0.5)
				circle.Transparency = 0
				button1.Transparency = 1
				wait(0.5)
				circle.Transparency = 1
				wait(0.5)
				circle.Transparency = 0
				button4.Transparency = 1
				wait(0.5)
				circle.Transparency = 1

				wait(1)

				tweenservice:Create(script.Parent.Door, TweenInfo.new(5, Enum.EasingStyle.Quad, Enum.EasingDirection.InOut), {Position = script.Parent.Door.Position + Vector3.new(0,7,0)}):Play()
				tweenservice:Create(script.Parent.Screen, TweenInfo.new(5, Enum.EasingStyle.Quad, Enum.EasingDirection.InOut), {Position = script.Parent.Screen.Position + Vector3.new(0,7,0)}):Play()
				wait(10)
				tweenservice:Create(script.Parent.Door, TweenInfo.new(5, Enum.EasingStyle.Quad, Enum.EasingDirection.InOut), {Position = script.Parent.Door.Position + Vector3.new(0,-7,0)}):Play()
				tweenservice:Create(script.Parent.Screen, TweenInfo.new(5, Enum.EasingStyle.Quad, Enum.EasingDirection.InOut), {Position = script.Parent.Screen.Position + Vector3.new(0,-7,0)}):Play()
				wait(6)
				selectedPattern[1].BrickColor = BrickColor.new("Institutional white")
				selectedPattern[2].BrickColor = BrickColor.new("Institutional white")
				selectedPattern[3].BrickColor = BrickColor.new("Institutional white")
				selectedPattern[4].BrickColor = BrickColor.new("Institutional white")
				circle.BrickColor = BrickColor.new("Institutional white")
				stage = 1
				debounce = false
				print("End of script")

			else
				trigger()
			end
			-- Disconnect All Connections
			for i, Connection:RBXScriptConnection in pairs(AllConnections) do
				Connection:Disconnect()
			end

		end)
		table.insert(AllConnections,Pattern4Connection)
	end
end)

the code above should be fixed, please give me an update.

1 Like

Yes!! Thank you so much for helping me. I really appreciate it!

One more thing, how do I make something happen if none of the buttons are pressed within 30 seconds? If I put a wait, it will halt the entire script. I believe coroutines are the answer to this problem, but I don’t know how to use them very well.

And just to clarify, when I say “make something happen,” I mean run a function