Timer in LUA (need help)

Hey i want to do a timer and it triggers an event once the timer fellt to 0. I did this but it doesn’t work

 while temps>0 do
            Wait(1)
            temps=temps-1
            TriggerEvent('chatMessage', "test", {255, 0, 0}, " Temps restant : "..temps.." secondes")
end 
TriggerEvent("unjail")

The chat message never pop

2 Likes

Have you put this in it’s own thread? As for the chat message not showing it should, assuming this code is being ran on the client side (or it could be failing because you’re calling it too much too fast). You could always try something like

Citizen.CreateThread(function()
    local time = 10 -- 10 seconds
    while (time ~= 0) do -- Whist we have time to wait
        Wait( 1000 ) -- Wait a second
        time = time - 1
        -- 1 Second should have past by now
    end
    -- When the above loop has finished, 10 seconds should have passed. We can now do something
    TriggerEvent("unjail") -- Call the client event "unjail"
end)
6 Likes

It needs to be in a thread ? Or it can be in a simple event ?

1 Like

If you’re using Citizen.Wait then it must be in a Thread.

3 Likes

I gotta ask, im looking for something like this for my home server. Did you get this to work?

Do people even read, this is a 4 year old topic