First you need to make sure you understand what a thread is.
Are you familiar with synchronous and asynchronous?
Normally code will run in a synchronous fashion, meaning it runs in order and when calling a function for example, it waits for the result/return, then continues. Like this;
Code:
function Example()
Wait(1000)
print("second")
end
print("first")
Example()
print("third")
Output:
first
second
third
Despite the function “Example” has a wait of 1 second, the code will wait for it to return, before calling it’s own print afterwards.
A thread is asynchronous, in the eyes of the whole script, meaning that anything inside it can run at the same time as anything outside it. If you call a function and that function has a thread inside it with everything in that thread, or you call a function inside a thread, the code keeps running, while the code in the thread runs at the same time. It doesn’t wait for it to return. Like this;
Code:
function Example()
Wait(1000)
print("second")
end
print("first")
CreateThread(function()
Example()
end)
print("third")
Output:
first
third
second
See how it printed out of order? That’s because the function call was inside a thread this time, so the code kept running and didn’t have to wait 1 second to print “third” so it did that, then the thread wait finished and printed “second”.
More info here → Citizen.CreateThread - Cfx.re Docs
Now, note that I just used CreateThread and Wait, not Citizen.CreateThread or Citizen.Wait. You don’t have to use the Citizen.X because they work the same without them. So make your code neater by leaving these out.
Now about the Wait() function. This will yield (pause) your code, as you’ve seen. Most often, they are used inside threads, because something is triggered and you want to pause at some point before continuing, while still keeping the rest of the script running. However there are times that they are used outside of threads to pause everything. It’s case by case. In terms of what integer to use, they are milliseconds. So Wait(1000) is 1 second and Wait(1) is 1 millisecond. Wait(0) is basically 1 frame. You mentioned that someone told you to always use a Wait(int) inside a thread. That’s not true. Always use it inside a loop though.
This will crash your game;
CreateThread(function()
while true do
print("don't do this")
end
end)
This won’t;
CreateThread(function()
while true do
Wait(0)
print("don't do this")
end
end)
The second thread won’t crash your game like the first, because instead of running as fast as possible, using up 100% of your processing power, using Wait(0) will run every frame, just like many other things do. However, if something doesn’t need to run every frame, increase that number to suit your needs. Want something checking your coordinates and don’t need frame perfect precision? Try every 1-3 seconds (1000-3000ms). Stuff like that.
So when they said to always use a wait, they were likely trying to help you avoid crashing your game with loops lacking a wait. And they mentioned using it inside all threads, because most loops (definitely infinite ones) are usually inside threads. But there is nothing wrong with something like these 2 examples;
Thread with no wait:
CreateThread(function()
print("Hope you learnt something!")
end)
Finite loop with no wait and no thread:
function Example()
for i = 1, 5 do
print(i)
end
end
Example()