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)