Help with Wait()

Hello, I’ve been trying to create a script command that does something and then, after a specific time, exactly 1 second, it does something else. However, whenever I tried this, it always executed what was before the “Wait” command, and all the code after the “Wait” doesn’t work. I would like to know why this is happening and if there’s a way to fix it. Additionally, since nothing was happening and I wasn’t getting any errors either, is there something in FiveM that I can use to check the errors that might be occurring or view them in a more detailed way to understand what’s happening and not be in the dark?

Thank you very much in advance.


RegisterCommand("updatec", function(source, args, rawCommand)
    print("before")
    Wait(1000)
    print("after")
end, true)

Hmmm - I’m not sure why this may be happening, but here may be some additional troubleshooting points:

  1. Test this out with AND without enabling lua5.4 in your fxmanifest.
    1a. If you’re using __resource.manifest, now’s a good time to switch to test it out
  2. Try this particular block of code (the prints) outside of the registercommand function
  3. Try this particular block of code in a generic function
  4. Try enclosing this block of code in a thread
  5. Increase the wait time to something ridiculous
  6. Stop server, clear server cache, restart client (optionally clear client cache) and try again;

From the docs:

Citizen.Wait

This will “pause” the execution of the current thread for miliseconds amount of time.


So to expect correct “Wait()” behaviour, you’ll need to create a thread inside of your RegisterCommand like so:

RegisterCommand(..., function(...)
    CreateThread(function()
        -- your code
        Wait(1000) -- wait 1 second
        -- more code
    end)
end, ...)

Edit: I may be wrong since the RegisterCommand could also already create a courutine environment