Hello, I have a script that creates new threads using spawn() in a while true loop:
function performTest()
local t = 0
local count = 0
local canAdd = true
uis.InputBegan:Connect(function(input)
if input.UserInputType == Enum.UserInputType.MouseButton1 and canAdd then
count = count + 1
end
end)
wait(1)
canAdd = false
cps.Value = count
end
while wait(0.1) do
spawn(performTest) -- this is what im worried about.
end
I am wondering whether or not this will cause the game to lag, crash, or do something of that sort - because I heard that you can create a maximum of about 30 threads. I was trying to find a way to “remove” a thread, so that it doesn’t run when the function is done executing.
So, will doing this affect the game? If so, how do I work around it?
Well. You should try to use the least “threads” as possible. I never heard of a limit. “Threads” in roblox work differently, I believe they wait until your script yields, and then when it yields, it runs them.
Spawn is I believe, less demanding than (coroutine), except people don’t really like using spawn. Coroutines are way more efficient.
Spawn should be used for less demanding work which doesn’t require precision, or anything like that. For example, a clean up loop inside a module/script.
There is no such thing. A thread will “close” when it’s code has been finished. In this case, it would be about 1 second after you called it, since there’s a wait(1) there.
With what you have here? No. Ten threads at a time (10/second that all die after 1 second) doing just the code you’ve shown is not going to impact performance.
@LucasTutoriaisSaimo is right though, and creating that many threads is maybe indicative of a design that could be improved.
Depends what you’re trying to do. Event-based coding when you’re able. Different solutions if you’re not.
spawn functions lets you run code without yielding. However spawn has a built-in wait before running the function, use coroutine.wrap or coroutine.resume(coroutine.create()) instead because they run the function the time it was called.
Thank you, @LucasTutoriaisSaimo! This is the exact information I was trying to determine for a long time. I know that using spawn() isn’t the most efficient way of doing things - but I was just trying to make sure that spawning 10 functions a second wouldn’t hurt the performance of the game. I was also wondering whether or not the “thread” I spawned closes automatically after it finishes executing. So, thanks, this is just what I was looking for.
Also, for some reason, I misunderstood “threads” in Roblox, and I thought that each time you use spawn(), it allocates another thread of your CPU. But now I understand how these “threads” work.
Okay, thanks for the suggestion, @Lielmaster! I just don’t quite understand how coroutines work yet, since I’m used to using spawn() in almost everything - but I will look into it.
i am just going to explain you the basic, and why you should use it instead of spawn().
Coroutines uses threads, you can play/resume it, yield it or anything. But if you just need to run a function without yielding, use coroutine.wrap.
local f = coroutine.wrap(function()
wait(0.5)
print("Second to print") -- Prints Second
end)
f()
print("First to print") -- Prints First
f() -- Errors because the thread is already running
just remember that it is a thread, and once the thread is running, you will need to create another. iirc once a thread is “dead” or finished, it will be deleted or something
just to show you why use coroutine instead of spawn, run this code:
spawn(function()
print("Second to print") -- Prints Second
end)
coroutine.wrap(function()
print("First to print") -- Prints First
end)()
Thank you, @Lielmaster, that really helps. For my case, I should really be using coroutines, since I need the function I’m running to run at precisely 10 times a second.