Stopping while loop in spawn function

How would I make the spawn function stop flickering the lights when the blackout is over?

local children = workspace:GetDescendants()

local BlackoutValue = script.Parent:FindFirstChild("Blackout")
local BlackoutAlarm = game.SoundService.BlackoutAlarm
local RepairPrompt = script.Parent:FindFirstChild("ProximityPrompt", true)

function blackout()
	RepairPrompt.Enabled = true
	BlackoutAlarm:Play()
	for i, part in pairs(children) do
		if part:IsA("Part") and part.Name == "Light" then
			local light = part:FindFirstChildWhichIsA("SpotLight")
			light.Enabled = false
		end
		
		if part:IsA("Part") and part.Name == "EmergenyLight" then
			local light = part:FindFirstChildWhichIsA("SurfaceLight")
			spawn(function()
				while true do
					light.Enabled = not light.Enabled
					wait(.4)
				end
			end)
		end
	end
	
	
end


function TurnLightsOn()
	BlackoutAlarm:Stop()
	RepairPrompt.Enabled = false
	for i, part in pairs(children) do
		if part:IsA("Part") and part.Name == "Light" then
			local light = part:FindFirstChildWhichIsA("SpotLight")
			light.Enabled = true
		end
		
		if part:IsA("Part") and part.Name == "EmergenyLight" then
			local light = part:FindFirstChildWhichIsA("SurfaceLight")
			light.Enabled = false
		end
	end
end

RepairPrompt.Triggered:Connect(function(player)
	TurnLightsOn()
end)

while wait(1) do
	local randomNumber = math.random(1,4)
	print(randomNumber)
	if randomNumber == 1 and BlackoutValue.Value == false then
		BlackoutValue.Value = true
		blackout()
	end
end
local children = workspace:GetDescendants()

local BlackoutValue = script.Parent:FindFirstChild("Blackout")
local BlackoutAlarm = game.SoundService.BlackoutAlarm
local RepairPrompt = script.Parent:FindFirstChild("ProximityPrompt", true)
local isBlackout = false

function blackout()
	RepairPrompt.Enabled = true
	BlackoutAlarm:Play()
	for i, part in pairs(children) do
		if part:IsA("Part") and part.Name == "Light" then
			local light = part:FindFirstChildWhichIsA("SpotLight")
			light.Enabled = false
		end
		
		if part:IsA("Part") and part.Name == "EmergenyLight" then
			local light = part:FindFirstChildWhichIsA("SurfaceLight")
                        local isBlackout = true
			spawn(function()
				while isBlackout do
					light.Enabled = not light.Enabled
					wait(.4)
				end
			end)
		end
	end
	
	
end


function TurnLightsOn()
        isBlackout = false
	BlackoutAlarm:Stop()
	RepairPrompt.Enabled = false
	for i, part in pairs(children) do
		if part:IsA("Part") and part.Name == "Light" then
			local light = part:FindFirstChildWhichIsA("SpotLight")
			light.Enabled = true
		end
		
		if part:IsA("Part") and part.Name == "EmergenyLight" then
			local light = part:FindFirstChildWhichIsA("SurfaceLight")
			light.Enabled = false
		end
	end
end

RepairPrompt.Triggered:Connect(function(player)
	TurnLightsOn()
end)

while wait(1) do
	local randomNumber = math.random(1,4)
	print(randomNumber)
	if randomNumber == 1 and BlackoutValue.Value == false then
		BlackoutValue.Value = true
		blackout()
	end
end

Thanks however it didn’t work because of

So I changed it to isBlackout = true

1 Like

Instead of removing the local, it’s better to move the local declaration to a location where it can be shared by everything that needs it. I.e. if everything needs it, put local isBlackout at the top of the script.

You are right, that was my bad. I am typing on a phone :face_holding_back_tears:

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.