Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How to fix my issue with displaying a countdown on a GUI?

Why does this happen every time the round reaches round 3 or higher? How can I fix my code:

SendRoundInfoEvent.OnClientEvent:Connect(function(rapper1, timerr, round)
    local serverMessage = script.Parent.Parent.ServerMessage
    local timerDisplay = script.Parent.Parent.TimerDisplay
    local timer = 30
    
    local function displayAppropriateThings(roundNum)
        serverMessage.Text = rapper1.." is rapping"

        local initalTimerValue = timer
        repeat wait(1) initalTimerValue -= 1 timerDisplay.Text = initalTimerValue.." Remaining" until roundInformation["Round"..tostring(roundNum)].Value == true
    end
    
    
    if round == "Round 1" then
        displayAppropriateThings(1)
        
    elseif round == "Round 2" then
        displayAppropriateThings(2)
        
    elseif round == "Round 3" then
        displayAppropriateThings(3)
            
    elseif round == "Round 4" then
        displayAppropriateThings(4)
    end
end)

1 answer

Well, it looks like that the repeat until loop ain't stopping and continuing to go below zero, and I assume that the function is fired again when it becomes 0, but the previous loop was still going on (maybe because the roundInformation value doesn't become true). So, the Text was showing both the previous loop text as well as just triggered loop text.

So, how can you fix it?

Well, here's an edited version of your script with explanation :

 repeat wait(1) 
initalTimerValue -= 1 
timerDisplay.Text = initalTimerValue.." Remaining" 
until roundInformation["Round"..tostring(roundNum)].Value == true or initialTimerValue == 0 -- Automatically stops the loop when the timer reaches 0. Eliminating the chances of displaying of negative numbers.

Lemme know if it helps!

This discussion is preserved as it was. New answers, comments, and votes are closed.