How do I run multiple while loops in this script for a FNAF Power System

I’m attempting to create a five nights at freddy’s power system (Slowly Ticks down power and activating doors decreases power further. once power hits 0 the loops stop)

The issue is that I need to run multiple While Loops at once in the same script, I’ve tried using coroutines but the power only ticks down once because I must be using them wrong (I can’t tell how to fix it because the script is getting too complex for me who’s relatively new to scripting

Can anyone give me advice or potentially fix my script?

Script Without Coroutines

print("Running Power Mainframe")

local RunService = game:GetService("RunService")
---
local LeftDoorOn = game.Workspace.DoorSystem.WestDoor.DoorOn
local RightDoorOn = game.Workspace.DoorSystem.EastDoor.DoorOn
---
local PowerConsumption = game.ServerStorage.PowerConsumption
local PowerLeft = game.ServerStorage.PowerLeft
local PowerOn = game.ServerStorage.PowerOn
---
local PowerText = game.StarterGui.PowerUI.Frame.Pow
---
local PowerOutageFX = game.Lighting.OutageCorrection
local PowerOutageSFX = game.Workspace.PowerOutage
---

print("Locals Set Up")

function UpdateText()
	print("Attempting to Update Text")
	PowerText.Text = PowerLeft.Value
	print("Power left =", PowerLeft.Value)
end

function PowerMain()
	if PowerOn.Value == true then
		PowerOutageFX.Enabled = false
		print("Calling Power Outage Check")
		PowerOutageCheck()
		print("Power is On and calling base drain")		
		BaseDrain()
		print("Calling Door Drain")
		DoorDrain()
	else
		PowerOutage()
	end	
end


function DoorDrain()
	while true do
		if PowerOn.Value == true then
			print("Power is on for door drain")
			if LeftDoorOn.Value == true then
				print("Power detected left door is on")
				PowerLeft.Value = PowerLeft.Value - 1
				print("Calling Update Text")
				UpdateText()
			end
			if RightDoorOn.Value == true then
				print("Power detected right door is on")
				PowerLeft.Value = PowerLeft.Value - 1
				print("Calling Update Text")
				UpdateText()
			end
		else
			print("Breaking DoorDrain Loop")
			break
		end
		wait(1)
	end

end

function BaseDrain()
	while true do
		if PowerOn.Value == true then
			print("Waiting Power Consumption")
			wait(PowerConsumption.Value)
			print("Reducing Power due to Base Drain")
			PowerLeft.Value = PowerLeft.Value - 1
			print("Calling Update Text")
			UpdateText()
		else
			print("Breaking BaseDrain Loop")
			break
		end
		

	end
end

function PowerOutageCheck()
	while true do
		if PowerOn.Value == true then
			if PowerLeft.Value == 0 then
				PowerOn.Value = false
				print("Power is no longer on")
			else
				print("Power is On")
			end
		else
			print("Power has been shut down.")
			break
		end
		wait(1)
	end

end

function PowerOutage()
	PowerOutageFX.Enabled = true
	PowerOutageSFX:Play()
	---
	---
end

PowerMain()

Script With Coroutines

print("Running Power Mainframe")

local RunService = game:GetService("RunService")
---
local LeftDoorOn = game.Workspace.DoorSystem.WestDoor.DoorOn
local RightDoorOn = game.Workspace.DoorSystem.EastDoor.DoorOn
---
local PowerConsumption = game.ServerStorage.PowerConsumption
local PowerLeft = game.ServerStorage.PowerLeft
local PowerOn = game.ServerStorage.PowerOn
---
local PowerText = game.StarterGui.PowerUI.Frame.Pow
---
local PowerOutageFX = game.Lighting.OutageCorrection
local PowerOutageSFX = game.Workspace.PowerOutage
---

print("Locals Set Up")

local PowerOutage = coroutine.create(function()
	PowerOutageFX.Enabled = true
	PowerOutageSFX:Play()
	---
	---
end)

local UpdateText = coroutine.create(function()
	print("Attempting to Update Text")
	PowerText.Text = PowerLeft.Value
	print("Power left =", PowerLeft.Value)
end)

local PowerOutageCheck = coroutine.create(function()
	while true do
		if PowerOn.Value == true then
			if PowerLeft.Value == 0 then
				PowerOn.Value = false
				print("Power is no longer on")
			else
				print("Power is On")
			end
		else
			print("Power has been shut down.")
			break
		end
		wait(1)
	end

end)



local DoorDrain = coroutine.create(function()
	while true do
		if PowerOn.Value == true then
			print("Power is on for door drain")
			if LeftDoorOn.Value == true then
				print("Power detected left door is on")
				PowerLeft.Value = PowerLeft.Value - 1
				print("Calling Update Text")
				coroutine.resume(UpdateText)
			end
			if RightDoorOn.Value == true then
				print("Power detected right door is on")
				PowerLeft.Value = PowerLeft.Value - 1
				print("Calling Update Text")
				coroutine.resume(UpdateText)
			end
		else
			print("Breaking DoorDrain Loop")
			break
		end
		wait(1)
	end

end)

local BaseDrain = coroutine.create(function()
	while true do
		if PowerOn.Value == true then
			print("Waiting Power Consumption")
			wait(PowerConsumption.Value)
			print("Reducing Power due to Base Drain")
			PowerLeft.Value = PowerLeft.Value - 1
			print("Calling Update Text")
			coroutine.resume(UpdateText)
		else
			print("Breaking BaseDrain Loop")
			break
		end
		

	end
end)





local PowerMain = coroutine.create(function()
	if PowerOn.Value == true then
		PowerOutageFX.Enabled = false
		print("Calling Power Outage Check")
		coroutine.resume(PowerOutageCheck)
		print("Power is On and calling base drain")		
		coroutine.resume(BaseDrain)
		print("Calling Door Drain")
		coroutine.resume(DoorDrain)
	else
		coroutine.resume(PowerOutage)
	end	
end)


coroutine.resume(PowerMain)

Rather than using coroutine.create, I suggest using coroutine.wrap, as it creates a callable function.

With coroutine.wrap, you can call it immediately or after you create it, since the datatype returned is a function.

Learn more about coroutines here.

Note: I am answering this on phone, so I am unable to compile and check. It should work.

print("Running Power Mainframe")

local RunService = game:GetService("RunService")
---
local LeftDoorOn = game.Workspace.DoorSystem.WestDoor.DoorOn
local RightDoorOn = game.Workspace.DoorSystem.EastDoor.DoorOn
---
local PowerConsumption = game.ServerStorage.PowerConsumption
local PowerLeft = game.ServerStorage.PowerLeft
local PowerOn = game.ServerStorage.PowerOn
---
local PowerText = game.StarterGui.PowerUI.Frame.Pow
---
local PowerOutageFX = game.Lighting.OutageCorrection
local PowerOutageSFX = game.Workspace.PowerOutage
---

print("Locals Set Up")

local PowerOutage = coroutine.wrap(function()
	PowerOutageFX.Enabled = true
	PowerOutageSFX:Play()
	---
	---
end)

local UpdateText = coroutine.wrap(function()
	print("Attempting to Update Text")
	PowerText.Text = PowerLeft.Value
	print("Power left =", PowerLeft.Value)
end)

local PowerOutageCheck = coroutine.wrap(function()
	while PowerOn.Value do
			if PowerLeft.Value == 0 then
				PowerOn.Value = false
				print("Power is no longer on")
			else
				print("Power is On")
			end
		wait(1)
	end

end)



local DoorDrain = coroutine.wrap(function()
	while PowerOn.Value do
			if LeftDoorOn.Value == true then
				print("Power detected left door is on")
				PowerLeft.Value = PowerLeft.Value - 1
				print("Calling Update Text")
				UpdateText();
			end
			if RightDoorOn.Value == true then
				print("Power detected right door is on")
				PowerLeft.Value = PowerLeft.Value - 1
				print("Calling Update Text")
				UpdateText();
			end
		wait(1)
	end

end)

local BaseDrain = coroutine.wrap(function()
	while PowerOn.Value do -- avoid infinite loops
			wait(PowerConsumption.Value)
			print("Reducing Power due to Base Drain")
			PowerLeft.Value = PowerLeft.Value - 1
			print("Calling Update Text")
			UpdateText();
		end
		

	end
end)





local PowerMain = coroutine.wrap(function()
	if PowerOn.Value == true then
		PowerOutageFX.Enabled = false
		print("Calling Power Outage Check")
		PowerOutageCheck();
		print("Power is On and calling base drain")		
		BaseDrain();
		print("Calling Door Drain")
		DoorDrain();
	else
		PowerOutage();
	end	
end)


PowerMain();
2 Likes

After I made this post I attempted to do corountine.wrap, failing then I looked over your code and realised I wrote mine wrong.

With the combination of this and my old code I’ve managed to get it working!
(Although I now need to finish the code that I havent done (checking lights, and power outage turning lights off and shutting doors)

Thanks for your help!