[Need Guidance] Thread Control

One way of removing “all” threads is to have just a single thread running and checking those things that cannot be otherwise checked and then triggering an event that in turn all other resources can use with an event handler.

E.g. the default “baseevents” resource triggers an event when entering and leaving a vehicle. That means you can just have an event handler in all other scripts.

But there are a lot of game events that can also be used. See here: Some Game Events and how to use them

Example on how I used the game event CEventNetworkPlayerEnteredVehicle to start a thread that will terminate itself once the vehicle is no longer the one the player is in:

AddEventHandler("gameEventTriggered", function(eventName, eventData)
	if (eventName ~= "CEventNetworkPlayerEnteredVehicle") then return end

	local ped = PlayerPedId()
	local vehicle = eventData[2]

	while (DoesEntityExist(vehicle) and GetVehiclePedIsIn(ped) == vehicle) do
		-- do whatever you need to do

		Wait(0)
	end
end)

This of course still implies a thread for every script that uses this type of behavior, however it is only actually started when it’s needed and stopped when no longer needed.
I used this event since I like staying independent of third party resources.