Would many small loops be better than one big loop?

I have a building game were each block needs to have a certain action done to them, such as checking its hp.

to so this Im tagging all the different types of blocks with collectionservice, but ive come to a point were I need to determine the best approach to this.

I can (1) tag each block type and make mutliple loops to handle each block type, or (2) tag all blocks and handle all of them with one big loop.

heres an example of what im doing inside the loop, for this example I have it set as option 2:

local cs = game:GetService("CollectionService")


function managehp(p)
local hpmod = require(game.ServerScriptService.Modules.Blocks.HealthHandler)
hpmod.CheckBlock(p)
end




game.ReplicatedStorage.BattleMode.Changed:Connect(function(newval)
if newval == "true" then

for _, p in pairs(cs:GetTagged("NormalBlockTag")) do
wait(0.08)
print("setting block connections...")
if p.PrimaryPart then

p.Health.Changed:Connect(function()
managehp(p)
end)



end
end

end
end)

every time a round begins(every 30 or 60 seconds), a changed event is added to each and every tagged instance, that way I don’t have to constantly loop in order to check a blocks hp.

would this be wise? and should I add multiple loops to handle different block groups?

1 Like

Depends on what you want to achieve, if you’re just gonna use one big loop and fill it with even more loops creating a mess, you’d be better off using functions and other stuff to help you up with the cleaning.

But then again, it really depends on what you plan to achieve.

I suggest not filling the server with pointless tasks unless you need to:

use the server for security against cheaters.

You can also use FireClient and FireAllClients.

im just trying to check the hp for blocks built by players, the reason im asking this question is because overtime there will be a large amount of blocks, right now there’s no lag and what u saw does work,but ive only tested with two players that have only placed around 20 blocks. eventually there will be 8 players playing each with 100+ blocks being handled.

using collectionservice to connect changed events to these blocks means the only time any actions are taking place is if the block hp is being lowered which is a really good idea for keeping lag to a minimal.

but the loop itself looping through up to 1k in blocks could be problematic depending on the setup.
so would multiple loops splitting the job of setting changed events be better for this task? or should I stick with the single titan loop?

I think there should be no issue using the usual titan block, in the end it’s up to you to make your workplace fit your tastes, unless that style messes with Roblox’s optimization.

im gonna wait a bit to see if others post replies so I can get their input on this.

Why can’t you self-contain each block and act upon it with events? (I’m still a bit confused on what you are trying to do)

Edit: Depending on what you are trying to do, you can broadcast a message to all the blocks and have them respond however they need to using a bindable event.

if you mean why don’t I have the blocks manage themselves individually, its because I want to keep things clean by using collectionservice to manage all the blocks.

to try clearing things up:

the goal: use collectionservice to handle a group of blocks, and listen to their hp to check if its 0 while
keeping lag to a minimum when there are a lot of tagged blocks to loop through.

the problem: having a lot of blocks means the loop that is going through the tags will be going through over 1k in parts every 30 or 60 seconds. I would like to know if I should make multiple loops to handle different tagged blocks instead of handing all of them on the same tag.