##There are two solutions to your problem
Your script does not continue on because the while loop on lines 50 through 56 goes on
infinitely. If you make a while loop,(without breaking it) it will go on assuming you have made your while loop conditional to a delay(while wait() do) or true(while true do). You could use coroutines like this:
...
local turnOn = coroutine.wrap(function()
while wait(1) do
if script.Parent.IsOn.Value then
script.Parent.Hours.Value = script.Parent.Hours.Value - 2
end
end
end)
local isBroken = coroutine.wrap(function()
while wait(1) do
if script.Parent.Hours.Value <= 0 and not script.Parent.IsBroken.Value then
Light.Enabled = false
PopSound:Play()
script.Parent.IsBroken.Value = true
end
end
end)
turnOn()
isBroken()
I highly suggest taking a look at Roblox Wiki's documentation on coroutines here.
Now, this is a valid way to fix the script. However, if you were to add a lot of lights, your game would experience a lot of lag because of two while loops using if statements after a second. That will be multiplied with the numerous amount of lights you have. A way to optimize this is by adding the code inside of the second loop into the first loop. You could optimize this even more by adding a Changed event. Optimizing is just making code to be more efficient and faster.
Let's take a look at this event:
...
script.Parent.IsOn.Changed:Connect(function()
script.Parent.Hours.Value = script.Parent.Hours.Value - 2
if script.Parent.Hours.Value <= 0 and not script.Parent.IsBroken.Value then
Light.Enabled = false
PopSound:Play()
script.Parent.IsBroken.Value = true
end
end)
The difference between the Changed event and while loops is that while loops always run(in your case). However, the Changed event runs when a property has been changed. Since you are working with IntValues and BoolValues, we can assume that the only property that will be changed is the value(in your script of course).
Then, when the hour's value has been changed, we check if it is below or equal to 0 and if IsBroken is false (not script.Parent.IsBroken.Value is the same as script.Parent.IsBroken.Value == false and script.Parent.IsBroken.Value is the same thing as script.Parent.IsBroken.Value == true)
Next, we do your if statement that you have done in another loop and put it after subtracting Hour's value.
NOTE: IF THIS WAS NOT THE SOLUTION YOU WERE LOOKING FOR, PLEASE TELL ME. IF THE OPTIMIZED WAYS WERE NOT WHAT YOU WANTED, USE COROUTINES
Here is the final code:
local Switch1 = script.Parent.Switch1
local Switch2 = script.Parent.Switch2
local SwitchSound = script.Parent.Frame.Switch
local PopSound = script.Parent.Bulb.Pop
local Light = script.Parent.Bulb.Light script.Parent.Frame.FlickSwitch.MouseClick:Connect(function()
if not script.Parent.IsOn.Value then
if script.Parent.IsBroken.Value == false then
SwitchSound:Play()
Switch1.Transparency = 1
Switch2.Transparency = 0
Light.Enabled = true
script.Parent.IsOn.Value = true
print("Is On")
elseif script.Parent.IsBroken.Value == true then
SwitchSound:Play()
Switch1.Transparency = 1
Switch2.Transparency = 0
script.Parent.IsOn.Value = true
print("Is On but is broken")
elseif script.Parent.IsBroken.Value == false then
SwitchSound:Play()
Switch1.Transparency = 0
Switch2.Transparency = 1
Light.Enabled = false
script.Parent.IsOn.Value = false
print("Is Off")
else
SwitchSound:Play()
Switch1.Transparency = 1
Switch2.Transparency = 0
script.Parent.IsOn.Value = true
print("Is On but broken")
end
end
end)
script.Parent.IsOn.Changed:Connect(function()
script.Parent.Hours.Value = script.Parent.Hours.Value - 2
if script.Parent.Hours.Value <= 0 and not script.Parent.IsBroken.Value then
Light.Enabled = false
PopSound:Play()
script.Parent.IsBroken.Value = true
end
end)
I have left a few side notes.
You should use :WaitForChild(). This is extremely useful when you are not sure when an object has loaded or not.
You should store objects that you use constantly in variables. This can be extremely useful because you won't have to type the same thing again. You will simply have to type a word(or two) to simply access that object. This can also be useful when you are trying to debug or fix a script.
2 comments
Does this give an error?
No, all of his code is correct. Look at lines 50 through 65. There are two while loops. The second while loop wouldn't be reached because the first while loop is going on infinetly