Making a game where I want to continually spawn items that can be harvested by players. I’m planning on doing the following:
mouse.Button1Down:connect(function()
if harvestMouseTarget() then
wait(math.random(60,90))
respawnCollectible()
end
end)
I’m not really familiar with how having multiple of these threads running at the same time affects performance. My current understanding of events is that each time the player clicks the mouse button down, assuming he is hovering over a collectible it will create a new thread and wait to respawn the item again.
Will this code negatively affect performance if a ton of these threads are all waiting to respawn things simultaneously?
The delay and wait methods of doing this will have little to no difference between each other. Both will result in a coroutine being put on the scheduler to be resumed at a certain time. I suspect that wait will be more efficient/performant since delay has to create a new coroutine and start a new function, but that should not significantly affect your game.
Edit
Additionally, the performance impact of having many coroutines on the scheduler will be mostly on memory, and partially dependent upon what variables the coroutines have access to. The performance impact of having many coroutines resumed in a short time period should be insignificant alone; what the resumed scripts do will have a greater impact.
It looks like your coroutine maintains references to no variables and calls a single function. Assuming that function has little cost on its own and won’t be running multiple times a frame, it should be completely fine.
Technically correct. In practice, connect and Connect have no difference. Still, it’s important to avoid using deprecated methods and properties. connect is deprecated:
To summarize; threads (like anything else really) are a data type. They’ll sit around doing nothing until queried for it, which means performance issues in their numbers would likely be caused on the thread scheduler itself having to try keeping track.