Spawn or another script?

Hello! I was wondering what is more efficient, for example if I have two while loops like that:

while wait(1) do
    print("Hello")
end
while wait(0.5) do
    print("Hello2")
end

whats more efficient, should I use spawn function or should I move one of the while loops to another script?

Use spawn function is better than create new script.

Either way, you’re creating a new thread to run the code. I don’t think there’s a difference, performance-wise.

In this case, it comes down more to organization. Does it make sense to keep both loops in the same script? Are the loops related to each other or do they both need to access the same variables? Would it be cleaner to separate the functions across scripts?

I feel this is personal preference on how you want to organize your code.

2 Likes

It’s highly recommended to use coroutines over spawn().

5 Likes