Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

What's better, using multiple local scripts or multiple spawn() functions for separate tasks?

Just wondering, I have a game in which a lot of objects have their own individual tasks that animate them. For this, I control them all with a main local script, with a bunch of spawn functions to run them all (since they all use an infinite loop to keep them animating).


However, I'm unsure how reliable this is. Can using too many spawn functions in one script cause lag? Or would it be no different than just giving each object a local script that controls it's animation?

I really want to keep my code in one main script using the spawns, but not if there's a major deficiency factor that follows it.

1 answer

✓ Accepted answer

There won't be major difference between using spawn and new local scripts, the task scheduler threats them pretty much same. But you might run into an issue, where the most intensive loop will suppress others. I've noticed this in my game, where I have multiple update functions similar to yours, and if the script yields somewhere or a new Lua thread is made, it will become noticably slow.

I think that happens, because Lua has to schedule the new task or resume the thread, but it is having a hard time, because a hot loop is already hogging a lot of resources.

I think best way for tight, hot update loops is to use single thread that calls all updates one after other. That way you'll be certain that things are going to run consistently and have way more control of your code.

Example of my update loop:

local t = 0
local lastTick = tick()

game:GetService("RunService"):BindToRenderStep( "Update", Enum.RenderPriority.Last.Value, function()
    local newTick = tick()
    local dt = newTick - lastTick
    lastTick = newTick

    Projectiles:Update( dt, t )
    Animations:Update( dt, t )
    Camera:Update( dt, t )
end)

1 comment

0

Thanks for the insight, much appreciated.

This discussion is preserved as it was. New answers, comments, and votes are closed.