Jye R.
Jye R.

Reputation: 101

How to break out of a loop in x seconds

I am trying to modify a FiveM script and I am trying to break out of a loop in Lua after 4 seconds but I don't know how.

I don't know what I am doing and need some help.

Citizen.CreateThread(function()
    function drawscaleform(scaleform)
        scaleform = RequestScaleformMovie(scaleform)

        while not HasScaleformMovieLoaded(scaleform) do
            Citizen.Wait(0)
        end

        PushScaleformMovieFunction(scaleform, "SHOW_POPUP_WARNING")
        PushScaleformMovieFunctionParameterFloat(500.0)
        PushScaleformMovieFunctionParameterString("ALERT")
        PushScaleformMovieFunctionParameterString("~b~Peacetime Active")
        PushScaleformMovieFunctionParameterString("This Means No Priority Calls")
        PushScaleformMovieFunctionParameterBool(true)
        PushScaleformMovieFunctionParameterInt(0)
        PopScaleformMovieFunctionVoid()

        DrawScaleformMovieFullscreen(scaleform, 255, 255, 255, 255, 0)
    end

    while true do
        Citizen.Wait(0)
        drawscaleform("POPUP_WARNING")
    end
end)

I would like to break out of the while true after 4 seconds

Upvotes: 0

Views: 4491

Answers (2)

Doug Currie
Doug Currie

Reputation: 41180

There is a FiveM function Citizen.SetTimeout to call a function after a period has elapsed. Here is one (untested) way you could use it:

Citizen.CreateThread(function()
    function drawscaleform(scaleform)
    ...
    end

    local wait = true

    Citizen.SetTimeout(4000, function() wait = false end)

    while wait do
        Citizen.Wait(0)
        drawscaleform("POPUP_WARNING")
    end
end)

Upvotes: 1

brianolive
brianolive

Reputation: 1671

Most likely some combination of Lua’s break command, setting a condition on your while loop that more clearly communicates the loop’s intention (other than just while true...) and better using FiveM’s Citizen.Wait() function. The documentation for that function here says the argument is the number of milliseconds to pause the current execution thread.

Take some time to understand these elements, the code you are trying to modify, and experiment. SO won’t just code for you.

Upvotes: 1

Related Questions