Need Help for Activating/Deactivating Value

Hello

I am currently working on a game called “Classrooms,” a first person horror adventure game. I am currently scripting a fire alarm thing for the classrooms that will activate and deactivate with a number value. When I change the value to 0, I expect the alarm to stop, but the while true do loop breaks and the sound continues. I need help.

This here is my code if you need it (Ignore the typos)

if script.Parent.Alarm_Activated.Value == 1 then
		while true do
			script.Parent.Lieght.Material = Enum.Material.Neon
			script.Parent.Lieght.Fire_1.Enabled = true
			script.Parent.Lieght.Fkre_2.Enabled = true
			script.Parent.Glass.Material = Enum.Material.Neon
			script.Parent.Glass.Transparency = 0
			task.wait(0.11)
			script.Parent.Lieght.Material = Enum.Material.SmoothPlastic
			script.Parent.Lieght.Fire_1.Enabled = false
			script.Parent.Lieght.Fkre_2.Enabled = false
			script.Parent.Glass.Material = Enum.Material.Glass
			script.Parent.Glass.Transparency = 0.5
		task.wait(1)
		if script.Parent.Alarm_Activated.Value == 0 then
			break
		end
			if script.Parent.Alarm_Activated.Value == 0 then
				script.Parent.Speaker.Actual_Alarm:Stop()
				script.Parent.Lieght.Material = Enum.Material.SmoothPlastic
				script.Parent.Lieght.Fire_1.Enabled = false
				script.Parent.Lieght.Fkre_2.Enabled = false
				script.Parent.Glass.Material = Enum.Material.Glass
				script.Parent.Glass.Transparency = 0.5
			end
		end	
	end

View the DevLog for Classrooms: Working on a Game! | DevLog - #16 by TotallyBlackwolf

Changing the value to 1, should start the while loop, and change it to 0, should stop it? Like a switch? Should be useful if you use the Changed event, meanwhile
Try this:

if script.Parent.Alarm_Activated.Value == 1 then
	while true do
		script.Parent.Lieght.Material = Enum.Material.Neon
		script.Parent.Lieght.Fire_1.Enabled = true
		script.Parent.Lieght.Fkre_2.Enabled = true
		script.Parent.Glass.Material = Enum.Material.Neon
		script.Parent.Glass.Transparency = 0
		task.wait(0.11)
		script.Parent.Lieght.Material = Enum.Material.SmoothPlastic
		script.Parent.Lieght.Fire_1.Enabled = false
		script.Parent.Lieght.Fkre_2.Enabled = false
		script.Parent.Glass.Material = Enum.Material.Glass
		script.Parent.Glass.Transparency = 0.5
		task.wait(1)

		if script.Parent.Alarm_Activated.Value == 0 then
			script.Parent.Speaker.Actual_Alarm:Stop()
			script.Parent.Lieght.Material = Enum.Material.SmoothPlastic
			script.Parent.Lieght.Fire_1.Enabled = false
			script.Parent.Lieght.Fkre_2.Enabled = false
			script.Parent.Glass.Material = Enum.Material.Glass
			script.Parent.Glass.Transparency = 0.5
			break
		end
	end	
end

You could shorten the shutdown a bit as well:

		if script.Parent.Alarm_Activated.Value == 0 then
			script.Parent.Speaker.Actual_Alarm:Stop()
			break
		end

Thanks! That helped a lot :slight_smile:
CharLimit

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