Problem with while loop [SOLVED]

Still running into the same issue.

Not sure what I can do. The best solution I can think of is having the loop in a separate script and simply making the prompt turn it on and off. But I’m not sure how great of an idea that is.

Then we will have to loop the value status using :GetPropertyChangedSignal function.

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

-- Function to handle TV show playback
local function playTVShow()
    while not shouldStop do
        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)
    end
    script.Parent.Parent.Music:Stop()
end

-- Connect to the Status property change event
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
            playTVShow()
        end
    else
        -- Handle the case when Status is false
        -- Set the loop control variable to stop the loop
        shouldStop = true
        isPlaying = false  -- Reset the flag when the show stops playing
        -- You can add additional actions here, if needed
    end
end)

The same issue is still happening.

I think its best to just result to putting the loop in a separate script and having the prompt enable and disable that script.

I can’t think of any other way.

Thank for all your guys help though I really appreciate it, I’ll leave this post open incase one of guys has an idea in mind.

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

-- Function to handle TV show playback
local function playTVShow()
    while true do
        if shouldStop then
            break
        end
        
        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)
    end
    script.Parent.Parent.Music:Stop()
end

-- Connect to the Status property change event
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
            playTVShow()
        end
    else
        -- Handle the case when Status is false
        -- Set the loop control variable to stop the loop
        shouldStop = true
        isPlaying = false  -- Reset the flag when the show stops playing
        -- You can add additional actions here, if needed
    end
end)

I think we can make that the loop break immediately when shouldStop becomes true, regardless of where it is in the loop cycle. If this doesn’t work i can’t make myself any more ideas

To be honest, I don’t think a while true do loop is gonna work for this case. I’ve tried many ways to get to loop to actively check the status of the TV and none of it worked.

I got something for you :slightly_smiling_face: :

--your code...
local function tvshow()
   while Status.Value do
      --all the code of your TV
   end
end

local thread = nil
Status:GetPropertyChangedSignal("Value"):Connect(function()
  if Status.Value and not(thread) then
     thread = task.spawn(tvshow)
  elseif not(Status.Value) and thread then
    task.cancel(thread)
    thread = nil
  end
end)

Using threads to make your TV work should work as normal, but what’s useful here is that you can actually cancel a thread while it’s playing. So when Status.Value gets false in the GetProprtyChangedSignal, you can cancel the thread. Please let me know if this worked for you.

1 Like

Can you fix this because when I put it it’s missing a lot of ends. I tried fixing it but I’m not sure if its in the same vision that you made it so I rather let you fix it.

I think I fixed it rn, sorry for forgetting the "end"s !

Ah wow this actually worked perfectly. Thank you so much I really appreciate it. I never learned task.spawn() so I will need to take some time to figure out what it is and how it works. But once again thank you!

No problem ! It was fun trying to come up with a solution for your problem, also be sure to mark my post as a solution too !

3 Likes

Yes I know, thank you so much!

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