How to execute an action by touching parts in a specific order? (video + script)

Hello, I would like to modify the script below so that the action happens when I touch the two parts in a specific order (and if I touch them in the wrong order then nothing happens). The video shows that no matter what order I touch them in, the action happens. I have tried different scripts to fix this without success. Thanks

--Local Script in StaterCharacter
plr = game.Players.LocalPlayer

function onTouch(hit)
	game.Workspace.bon1.BrickColor = BrickColor.new("Bright red")
end
game.Workspace.bon1.Touched:Connect(onTouch)

function onTouch(hit)
	game.Workspace.bon2.BrickColor = BrickColor.new("Bright red")
end
game.Workspace.bon2.Touched:Connect(onTouch)

function onTouch(hit)
	if game.Workspace.bon1.BrickColor == BrickColor.new("Bright red") and game.Workspace.bon2.BrickColor == BrickColor.new("Bright red") then
		print("HIIIIIIIIIII")
		game.Workspace.bon1.BrickColor = BrickColor.new("Yellow flip/flop")
		game.Workspace.bon2.BrickColor = BrickColor.new("Yellow flip/flop")
	elseif game.Workspace.bon2.BrickColor == BrickColor.new("Bright red") and game.Workspace.bon1.BrickColor == BrickColor.new("Bright red") then
	end
end
game.Workspace.ouverture2.Touched:Connect(onTouch)
1 Like

Hello, can someone help me? (if not, I will try to put up my topic tomorrow)

I think there is shorter solution but i hope this will give you an idea on how to make it

--Local Script in StaterCharacter
plr = game.Players.LocalPlayer

local character = plr.Character or plr.CharacterAdded:Wait()

--Boolean
local PressOrder = 1 --Press Order Of Player
local Bon1Order = 1 --Order of part
local Bon2Order = 2 --Order of part

local Confirm_Cooldown = 1.5
local ConfirmPartInCooldown = false

local CorrectOrder = {
	["Bon1"] = false,
	["Bon2"] = false
}

game.Workspace.bon1.Touched:Connect(function(hit)
	--Touched Event can be fired it touch another part
	if not hit.Parent == character then --If hit isn't player character then return
		return
	end
	
	if CorrectOrder["Bon1"] then --No Need to check again if it already Pressed
		return 
	end
	
	game.Workspace.bon1.BrickColor = BrickColor.new("Bright red")
	
	if Bon1Order == PressOrder then
		CorrectOrder["Bon1"] = true
	end
	
	PressOrder += 1
end)


game.Workspace.bon2.Touched:Connect(function(hit)
	--Touched Event can be fired it touch another part
	if not hit.Parent == character then --If hit isn't player character then return
		return
	end
	
	if CorrectOrder["Bon2"] then --No Need to check again if it already Pressed
		return 
	end
	
	game.Workspace.bon2.BrickColor = BrickColor.new("Bright red")
	
	if Bon2Order == PressOrder then
		CorrectOrder["Bon2"] = true
	end

	PressOrder += 1
	
end)

game.Workspace.ouverture2.Touched:Connect(function(hit)
	--Touched Event can be fired it touch another part
	if not hit.Parent == character or ConfirmPartInCooldown then --If hit isn't player character then return
		print("Confirm Part Currently In Cooldown!")
		return
	end
	
	if CorrectOrder["Bon1"] and CorrectOrder["Bon2"] then --Check if both Boolean is true
		print("Right Order")
		
		game.Workspace.bon1.BrickColor = BrickColor.new("Yellow flip/flop")
		game.Workspace.bon2.BrickColor = BrickColor.new("Yellow flip/flop")
	else
		print("Wrong Order")
		game.Workspace.bon1.BrickColor = BrickColor.new("Black")
		game.Workspace.bon2.BrickColor = BrickColor.new("Black")
	end
	CorrectOrder["Bon1"] = false
	CorrectOrder["Bon2"] = false
	PressOrder = 1 --Reset Press Order
	
	ConfirmPartInCooldown = true
	task.wait(Confirm_Cooldown)
	ConfirmPartInCooldown = false
end)

You can ask if you confused or don’t understand

1 Like

I see that someone has already given an answer, but I thought I could give a shot at the solution as well.
I used a table that would collect the order in which you touch the parts. Then when you touch the “ouverture2” part, it makes sure the order is correct.

--Local Script in StaterCharacter
local plr = game.Players.LocalPlayer
local hitOrder = {}
local bon1 = workspace.bon1
local bon2 = workspace.bon2
local ouverture2 = workspace.ouverture2

bon1.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChildOfClass("Humanoid") then
		bon1.BrickColor = BrickColor.new("Bright red")
		if not table.find(hitOrder, bon1) then
			print("bon1")
			table.insert(hitOrder, bon1)
		end
	end
end)

bon2.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChildOfClass("Humanoid") then
		bon2.BrickColor = BrickColor.new("Bright red")
		if not table.find(hitOrder, bon2) then
			print("bon2")
			table.insert(hitOrder, bon2)
		end
	end
end)

ouverture2.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChildOfClass("Humanoid") then
		if bon1.BrickColor == BrickColor.new("Bright red") and bon2.BrickColor == BrickColor.new("Bright red") and #hitOrder > 0 then
			print("ouverture2")
			if hitOrder[1] == bon1 and hitOrder[2] == bon2 then
				-- code that runs when the sequence is done correctly
				print("Sequence was done correctly")
				bon1.BrickColor = BrickColor.new("Yellow flip/flop")
				bon2.BrickColor = BrickColor.new("Yellow flip/flop") 
			else
				print("Sequence was incorrect")
			end
		end
		table.clear(hitOrder)
	end
end)

Hope this helps!

2 Likes

Sequences.rbxl (35.4 KB)

Script
local Sequences = {}


local function RegisterSequence(SequenceObj)
	if SequenceObj.Registered == nil then return end
	SequenceObj.Registered = nil
	for PartIndex, Part in pairs(SequenceObj.Parts) do
		Part.Touched:Connect(function(hit)
			if PartIndex <= SequenceObj.Index then return end
			local Player = game.Players:GetPlayerFromCharacter(hit.Parent)
			if not Player then return end
			if SequenceObj.Index == -1 then return end
			if SequenceObj.Index+1 == PartIndex then
				if SequenceObj.Stepped(SequenceObj, Player, Part) then
					if PartIndex == #SequenceObj.Parts then
						SequenceObj.Complete(SequenceObj, Player, Part)
						if not SequenceObj.Index == -1 then SequenceObj.Index = 0 end
					else
						SequenceObj.Index = PartIndex
					end
				end
			else
				SequenceObj.Fail(SequenceObj, Player, Part)
				if not SequenceObj.Index == -1 then SequenceObj.Index = 0 end
			end
		end)
	end
end


local function AddSequence(Name, Parts, Stepped, Fail, Complete)
	local nSequenceObj = {}
	nSequenceObj["Name"] = Name
	nSequenceObj["Parts"] = Parts
	nSequenceObj["Stepped"] = Stepped
	nSequenceObj["Fail"] = Fail
	nSequenceObj["Complete"] = Complete
	nSequenceObj["Index"] = 0
	nSequenceObj["Registered"] = false
	table.insert(Sequences, nSequenceObj)
	RegisterSequence(nSequenceObj)
end




AddSequence("Rising",
	{
		workspace.p1,
		workspace.p2,
		workspace.p3,
		workspace.p4
	},
	function(this, Player, TouchPart) -- Stepped
		print("("..this.Name..") Sequence stepped: "..(this.Index+1).."/"..#this.Parts)
		TouchPart.Color = Color3.fromRGB(56, 62, 54) -- Stepped Color
		return true
	end,
	function(this, Player, TouchPart) -- Failed
		print("("..this.Name..") Sequence failed at: "..(this.Index+1).."/"..#this.Parts)
		this.Index = -1 -- Disable sequence
		coroutine.wrap(function()
			for _, p in pairs(this.Parts) do
				p.Color = Color3.fromRGB(154, 0, 2) -- Flash Red
			end
			wait(.5)
			for _, p in pairs(this.Parts) do
				p.Color = Color3.fromRGB(163, 162, 165) -- Regular Color
			end
			this.Index = 0 -- Enable sequence
		end)()
	end,
	function(this, Player, TouchPart) -- Completed
		this.Index = -1 -- Disable sequence
		print("("..this.Name..") "..Player.Name.." has completed the sequence")
		coroutine.wrap(function()
			for _, p in pairs(this.Parts) do
				p.Color = Color3.fromRGB(47, 154, 8) -- Flash Green
			end
			wait(.5)
			for _, p in pairs(this.Parts) do
				p.Color = Color3.fromRGB(56, 62, 54)  -- Disabled Color
			end
			wait(2)
			for _, p in pairs(this.Parts) do
				p.Color = Color3.fromRGB(163, 162, 165) -- Regular Color
			end
			this.Index = 0 -- Enable sequence
		end)()
	end
)


AddSequence("Falling",
	{
		workspace.p21,
		workspace.p22,
		workspace.p23,
		workspace.p24
	},
	function(this, Player, TouchPart) -- Stepped
		print("("..this.Name..") Sequence stepped: "..(this.Index+1).."/"..#this.Parts)
		TouchPart.Color = Color3.fromRGB(56, 62, 54) -- Stepped Color
		return true
	end,
	function(this, Player, TouchPart) -- Failed
		print("("..this.Name..") Sequence failed at: "..(this.Index+1).."/"..#this.Parts)
		this.Index = -1 -- Disable sequence
		coroutine.wrap(function()
			for _, p in pairs(this.Parts) do
				p.Color = Color3.fromRGB(154, 0, 2) -- Flash Red
			end
			wait(.5)
			for _, p in pairs(this.Parts) do
				p.Color = Color3.fromRGB(163, 162, 165) -- Regular Color
			end
			this.Index = 0 -- Enable sequence
		end)()
	end,
	function(this, Player, TouchPart) -- Completed
		this.Index = -1 -- Disable sequence
		print("("..this.Name..") "..Player.Name.." has completed the sequence")
		coroutine.wrap(function()
			for _, p in pairs(this.Parts) do
				p.Color = Color3.fromRGB(47, 154, 8) -- Flash Green
			end
			wait(.5)
			for _, p in pairs(this.Parts) do
				p.Color = Color3.fromRGB(56, 62, 54)  -- Disabled Color
			end
			wait(2)
			for _, p in pairs(this.Parts) do
				p.Color = Color3.fromRGB(163, 162, 165) -- Regular Color
			end
			this.Index = 0 -- Enable sequence
		end)()
	end
)

Hello, sorry for the late response, thank you for your help, indeed the version below is simplified. Thanks again for your help.

1 Like

Hello, sorry for the late response, Thank you very much for your help, your script works perfectly. Thanks again!