How would i optimize my game? [Threads]

Currently in my game, i’ve a few optimizations made, such as things that require a loop, run off of a single loop in a script connected via module scripts.

However, I need to optimize it more as i’m going to add more features that are going to add an extra load.

At the moment i’ve a bunch of scripts that run off of collection service.

image

Everything that’s not in LoopTied has a touched function within a for i,v in pairs loop, Is there a way to optimize it?

Also any other ideas / tips to optimize would be helpful also, Thanks.

I do not really understand what needs optimizing. Loops are not inherently bad. Depends on what you are looping over.

It’s not the loops that need optimizing, In the scripts Attach, Hydrant and Teleport, i iterate through each item with a given tag and then check if it’s been touched, I’m wondering if there’s a way to optimize that in terms to reduce threads.

EDIT: My bad, i saw i put loops in the title, my mistake

yeah, why are you using for i, v in pairs? it’s called a generic loop, and not needed here; iterate through ipairs to improve a bit on speed for several reasons.

Can you give more information on what each script does? is each script’s purpose
just to detect whether items were touched?

2 Likes

It is good that you modularized it like that but for developing reasons not performance reasons. In the end what matters is the amount of listeners placed, not how they are placed.

You can try to reduce collisions by using the collision filtering Collisions | Documentation - Roblox Creator Hub. This can reduce the times the .Touched function is fired which can actually increase performance.

Anyways you do not need to worry about that holding you from scaling. Unless have a lot of .Touched functions firing all the time.

Each script detects either whether the item has been touched or if a child has been added to an item, What’s the difference between ipairs and pairs?

I know about collision filtering but i need all of these items to be able to collide and fire the touched function, That’s why i’m using collection service. Thank you for the ideas

You can use collision filtering to make sure that they do not fire .Touched functions when they collide with objects that you do not care.

For example instead of doing:

part.Touched:Connect(function(other)
       if other.Parent ~= someFolder then return
       ......

You can use collision filtering to just prevent .Touched firing alltogether. But that’s all you can do honestly.

Ohhh i get you now, i’ve done that already in my scripts, It just checks for a velocity of the part before executing any function

Thanks again for your time and help, much appreciated :+1: