What causes extremely long wait() time? Possible solutions?

My game uses wait() quite alot. For some reason it gets as long as 3 seconds at minimum while it’s intended to have a 0.03 second minimum. From my testing it seems to have some correlation to script activity, but lowering the intensity of the scripts seems to have no affect at all.
Is wait() time increased by thread count?
I’m beginning to think that it might be caused by having many threads active at once using the spawn() function, because the script that’s causing all of this is for npc AI. The wait() time increases right along with the number of npcs, but not with their intelligence or loop speed.

If this is true, what are some alternatives I could use instead of spawn to achieve the same effect without having hundreds of threads? Maybe I could use a separate script for each npc?
This could be a big breakthrough for my game, because I’ve been struggling with bloated wait() times for quite a while now.

Yes creating new threads will slow down yield times, you can sorta bandage it by using task.wait and task.defer in replacement of wait() and spawn() however that won’t fix the core issue, to fix the core issue you need to have a manager loop that ran on 1 thread and performing task for each thing you need

Example say you want to check if a AI has a enemy nearby instead of having 1 script per AI for detection you can simply do

while true do
    for _,Ai in ipairs(workspace.AiFolder:GetChildren()) do
        -- Check distance and assign enemy to each Ai here
    end
    task.wait()
end

Oh right, I haven’t really thought of doing it like that. Would processing all of the npcs in a single loop cause it to loop slower at all?

The computation will make the loop slower so if it takes 3 seconds to finish the computation for the first Ai then the 2nd ai won’t be running code until those 3 seconds are up, however its also important to note that making new threads doesn’t actually run code faster, since lua runs threads 1 at a time and not in a multi threaded situtation, so either way if you have a large amount of computation then its going to slow down regardless, its just that performing the same actions in several threads are always less efficient then doing it in a loop

Well, I’ve got some big script changes to work on today then. I hope this fixes the problem properly.

Optimally you would want to use RunService for all of your updates. Overall using wait() isnt optimal and your much better off just using an events :Wait() function if your using it to wait for something to be ready. In my current game I dont use any waits since everything thats needs to wait has an event( which isnt that many)

Yeah, but I much prefer to fix the root of the issue rather than work around it.

thats not a work around this is the the most optimal and best practice way to do it.

another thing you can do is basically make a cooldown in a loop that will check every frame the time between the current and last time a piece of code was ran

I’m actually quite surprised by how well this worked, the wait() times have gone straight back to normal. The npc script is just as slow, but the removal of the hundreds of threads has isolated the slowness to the script alone.
So now all I need to do is find a way to speed up the AI.

Provide the script in question so that you can be assisted.