Problem with while loop [SOLVED]

Hello. I am making a simple TV system where when you turn on the tv it plays a little show that is played through 4 images that change every 8 seconds. This is the code that makes it work.

Basically when a player interacts with the proximity prompt, it will change the status, if the status is false, it would make it true which would turn on the tv, vice versa.

The problem that I’m coming across with this while loop is that even when the Status value changes from true to false, the loop still plays. I checked prints to see if this was true and it was. I have no idea why this is occurring cuz I checked to make sure if the value was actually changing and it was.

I have no other idea why this could be happening so I’ve come here to hopefully get an answer. Any help is appreciated!

3 Likes

you sure, you making the Status False?? maybe do it like this


Status:GetPropertyChangedSignal("Value"):Connect(function()
	
	if Status.Value == true then
		
		while true do
			if Status.Value == true then
				--- rest of the code
			end
		end
	end
end)
2 Likes

That would create an infinite loop, you never exit the loop if Status.Value is false.

When you say the loop still plays, I just want to verify that you mean it starts the loop again, and not that the loop keeps going until the end. A while loop won’t stop until it reaches the end of the block AND the condition is false.

2 Likes

He have it like this, so i suggested that way.

Change it to this

Status:GetPropertyChangedSignal("Value"):Connect(function()
	
	if Status.Value == true then
		
		while true do
			if Status.Value == true then
				--- rest of the code
				
			elseif not Status.Value then
				break
			end
		end
	end
end)

breaking the loop should solve the problem.

2 Likes

To resolve this issue, you can use a while true loop and break out of it when the Status value changes to false. Here’s how you can modify your code:

Status:GetPropertyChangedSignal('Value'):Connect(function()
    if Status.Value == true then
        while true do
            -- Your code to play the TV show
            if Status.Value == false then
                break  -- Break out of the loop if Status changes to false
            end
        end
    else
        -- Handle the case when Status is false
        -- You can add code here to stop the TV show or perform other actions
        return
    end
end)

If Status changes to false , the loop will break out and the code execution will continue after the loop. This ensures that the TV show stops playing when Status changes to false.

2 Likes

I Tried this and it didn’t work, still having the same issue.

2 Likes

I can’t do this because the loop has a wait() in it, so it would take probably 20 seconds before the loop even reaches that if statement. So if the player turns the TV off and on really quickly, it wouldn’t reset the TV show like its supposed to.

2 Likes
If not Status.Value then break end

put this line after each wait(8). So its check before executing the rest of the code.

2 Likes

When the value turns false, the loop does still play but when it does reach the end, it stops. I’m trying to find a solution for how to just completely stop this loop regardless of where it is when the value is changed to false.

2 Likes

Instead of using a while loop with wait() inside, can’t you just get the time (using os.clock() I think), and since it’s in seconds, you can do something like so :

while Status.Value do
   local frames = -- put the number of frames for the TV you have here
   local frame = (((os.clock()%8) * frames) // 8) +1
   -- now you can do if statements for each frame, or use a function depending on variable frame
end

frame here is the number of the frame you can show on the TV.
You would have to offset os.clock() at the beginning of your code so that it shows the first frame for 8 seconds, and then it will eventually loop.

1 Like

this should work @6vrse (fillers fillers is annoying)

1 Like
local isPlaying = false  -- Variable to track if the TV show is currently playing

Status:GetPropertyChangedSignal('Value'):Connect(function()
    if Status.Value == true then
        -- Start playing the TV show only if it's not already playing
        if not isPlaying then
            isPlaying = true  -- Set the flag to indicate that the show is now playing
            repeat
                -- Your code to play the TV show
                -- This loop will continue until Status changes to false
                wait(1)  -- Adjust the wait time as needed
            until Status.Value == false
            isPlaying = false  -- Reset the flag when the show stops playing
        end
    else
        -- Handle the case when Status is false
        -- You can add code here to stop the TV show or perform other actions
        isPlaying = false  -- Reset the flag when the show stops playing
        return
    end
end)

You need to introduce a boolean variable isPlaying to track whether the TV show is currently playing.
Inside the loop where the TV show is played, we only enter the loop if isPlaying is false (meaning the show is not already playing). Once the loop starts, we set isPlaying to true.
When the Status changes to false, we check if isPlaying is true. If it is, we stop the show and reset isPlaying to false.
This approach ensures that the TV show resets immediately when the Status changes to false, regardless of the loop’s execution time.

lmk if this works

2 Likes

The Status variable is what I’m using to check if the TV is playing or not.

1 Like

This did not work either, still having the same issue.

1 Like

Fixed

local isPlaying = false  -- Variable to track if the TV show is currently playing

Status:GetPropertyChangedSignal('Value'):Connect(function()
    if Status.Value == true then
        -- Start playing the TV show only if it's not already playing
        if not isPlaying then
            isPlaying = true  -- Set the flag to indicate that the show is now playing
            while Status.Value do
                -- Your code to play the TV show
                wait(1)  -- Adjust the wait time as needed
            end
            isPlaying = false  -- Reset the flag when the show stops playing
        end
    else
        -- Handle the case when Status is false
        -- You can add code here to stop the TV show or perform other actions
        isPlaying = false  -- Reset the flag when the show stops playing
        -- Reset the TV show here if needed
    end
end)

1 Like

Let me give some more information about this issue.

Now the TV does work when the loop ends but here’s the thing.

When the status turns false, if the loop is only halfway through the code the loop will still continue. This is the problem I have. I want to make it where no matter where the loop is so far in the code, when the TV turns off, the loop also turns off. I tried seeing many ways to detect if the value is being turned off so the loop knows, but with the wait() being there it makes it nearly impossible to.

If there’s any other questions you guys have let me know.

1 Like

In that case you can create separate variables to control the loop. When Status changes to false, shouldStop is set to true, causing the loop to exit regardless of its current position.
This ensures that the loop will stop immediately when Status changes to false, even if it’s in the middle of its execution.

local isPlaying = false  -- Variable to track if the TV show is currently playing
local shouldStop = false -- Variable to control the loop termination

Status:GetPropertyChangedSignal('Value'):Connect(function()
    if Status.Value == true then
        -- Start playing the TV show only if it's not already playing
        if not isPlaying then
            isPlaying = true  -- Set the flag to indicate that the show is now playing
            shouldStop = false -- Reset the loop control variable
            while Status.Value and not shouldStop do
                -- Your code to play the TV show
                wait(1)  -- Adjust the wait time as needed
            end
            isPlaying = false  -- Reset the flag when the show stops playing
        end
    else
        -- Handle the case when Status is false
        -- You can add code here to stop the TV show or perform other actions
        shouldStop = true -- Set the loop control variable to stop the loop
        isPlaying = false  -- Reset the flag when the show stops playing
        -- Reset the TV show here if needed
    end
end)

So many comments but they are neccesary :slightly_smiling_face:

Hi, have you tried my solution and did it work ? It uses a very different approach, but at least allows you to break from the while loop almost instantly.

Sorry about that theres a lot of replies lol, I’ll get to yours if these don’t work.

This is how it should look, lmk if it works

local isPlaying = false  -- Variable to track if the TV show is currently playing
local shouldStop = false -- Variable to control the loop termination

Status:GetPropertyChangedSignal('Value'):Connect(function()
    if Status.Value == true then
        -- Start playing the TV show only if it's not already playing
        if not isPlaying then
            isPlaying = true  -- Set the flag to indicate that the show is now playing
            shouldStop = false -- Reset the loop control variable
            repeat
                -- Your code to play the TV show
                script.Parent.Parent.Music:Play()
                Screen.Frame1.Visible = true
                Screen.Frame4.Visible = false
                script.Parent.Parent.Sound.SoundId = Run
                script.Parent.Parent.Sound:Play()
                wait(8)
                
                Screen.Frame2.Visible = true
                Screen.Frame1.Visible = false
                script.Parent.Parent.Sound.SoundId = Scream
                script.Parent.Parent.Sound:Play()
                wait(8)
                
                Screen.Frame3.Visible = true
                Screen.Frame2.Visible = false
                script.Parent.Parent.Sound.SoundId = Eat
                script.Parent.Parent.Sound:Play()
                wait(8)
                
                Screen.Frame4.Visible = true
                Screen.Frame3.Visible = false
                wait(8)
            until shouldStop or not Status.Value -- Exit the loop if shouldStop is true or Status.Value becomes false
            isPlaying = false  -- Reset the flag when the show stops playing
            script.Parent.Parent.Music:Stop() -- Stop the music
        end
    else
        -- Handle the case when Status is false
        shouldStop = true -- Set the loop control variable to stop the loop
        isPlaying = false  -- Reset the flag when the show stops playing
        -- You can add additional actions here, if needed
    end
end)