Hello! I am Domderp999. I am currently making a timer with Pre Music. However, I am noticing that an if statement keeps looping. (Its looping because its all in a while loop). I dont know why… I use a way to make it stop after going through the if statement once, but it keeps going…
Why does it keep looping?
If you have further questions because I explained very little, ask in the reply section.
It keeps playing because you’ve set script.Parent.Music:Play()
, to stop it use script.Parent.Music:Stop()
But I don’t want it to stop. I want to fix the looping issue where the entire if loop loops more. notice how I tried putting a cooldown in the if statement
So what are you trying to do with the if statement.
The If statement makes the music play when the countdown gets a certain time.
It loops because each time it sets PlayingMusic as true, due to the while do
loop.
do you mean while true? or is it while do.
So when it runs the if statement, it sets PlayingMusic to false and then restarts the cycle by setting it back to true.
where does it set it back to true?
It sets it back to true at the beginning of the while do cycle, where you’ve put local PlayingMusic = true
Try putting local PlayingMusic = true
of the while do cycle, under local Stop = 0
But isn’t that part a Varible? And not an actual line of code?
If you will put it inside the while do cycle, once it will always result as true.
Explain a bit more. Im confused
local Stop = 0
local PlayingMusic = true
while wait(1) do . . .
This is what I mean. Put the local PlayingMusic = true
out of the while do
cycle.
Or you could use break
.
So that in the last if statement, you can stop the loop entirely. Like this:
if Time <= 116 and PlayingMusic == true then
PlayingMusic = false
script.Parent.Music:Play()
script.Parent.Music.TimePosition = 116 - Time
break -- Should stop the while loop
If neither @Synceratus’s solution or mine work, then it’s likely to do with the condition of the if statement not being met. A good test would be to use a print() to find out what “Time” is equal to outside of that if statement. Check if it ever becomes less than or equal to 116, and put it outside of the if statement but in the loop to check every second, with this:
while wait(1) do
if Stop == 0 then
local Time = 1598575200 - Tick()
-- Other code...
print(Time)
-- Other code...
Let me know what it prints if the solutions myself and @Synceratus don’t work first.