Attempting to make a multi-lever gate

Hello, I want to make a gate that operates on multiple levers that have to be pulled down, they are also timed.

I am unsure how I should approach this without writing several functions, if you could take a gander at this and point me in a direction I should take I would appreciate it.

if v.Name == "MultiGate" then -- Multi Lever Gate
		local leverGate = v
		
		local Levers = {
			leverGate.Lever1,
			leverGate.Lever2,
			leverGate.Lever3
		}

		local ggate = leverGate.Gate
		ggate.CanCollide = true
		local gorigin = ggate.CFrame
		local gtarg = gorigin*CFrame.new(0,ggate.Size.y-1,0)
		
		local lorigin1 = Levers.CFrame*CFrame.Angles(0,math.rad(0),math.rad(0))
		local ltarg1 = Levers.CFrame*CFrame.Angles(60,math.rad(0),math.rad(0))

		local gateClosed = true
		local switches = 0
		
		local function Lever(pl)
			local ch = pl and pl.Character
			if pl and not ch then return end
			local hum = ch and ch:FindFirstChild("Humanoid")
			if ch and not hum then return end
			if hum and hum.Health <= 0 then return end
			local curtime = tick()
			
			Levers.ClickDetector.MaxActivationDistance = 0
			for i = 0,1,0.05 do
				Levers.CFrame = lorigin1:lerp(ltarg1,i)
				wait()
			end
			Levers.CFrame = ltarg1
			switches += 1
			print("Plus one")
			wait(5)
			for i = 0,1,0.05 do
				Levers.CFrame = ltarg1:lerp(lorigin1,i)
				wait()
			end
			Levers.CFrame = lorigin1
			switches -= 1
			Levers.ClickDetector.MaxActivationDistance = 10
		end

		local function toggleGate()
			local curtime = tick()
			if switches ~= 3 then return end
			print("ToggleGate Confirmed")
			
			ggate.Sound:Play()
			if gateClosed then
				gateClosed = false
				for i = 0,1,0.05 do
					ggate.CFrame = gorigin:lerp(gtarg,i)
					wait()
				end
				ggate.CFrame = gtarg
				wait(5)
				ggate.Sound:Play()
				gateClosed = true	
				for i = 0,1,0.05 do
					ggate.CFrame = gtarg:lerp(gorigin,i)
					wait()
				end
				ggate.CFrame = gorigin
			end
			wait(0.5)
		end
		Levers.ClickDetector.MouseClick:connect(Lever)
		spawn(function()
			while true do
				toggleGate()
				wait()
			end
		end)
	end
1 Like

You could detect when each lever individually is pulled down. Then, when they are in a pulled-down state you can set an Instance Attribute to true. Then, when the lever goes back into the original state after the timer you can set the attribute back to false. Then you could use an and condition statement and check whether each lever is activated using Instance | Roblox Creator Documentation.

Just make the levers add the value to how much the levers are switched, then check if the value is 3 for example in the script, then open it.