Is there an efficient way of updating the same property of multiple instances?

Hi, I’m looking for the most efficient way to update the same property of multiple instances without expensively looping through each and every one of them.
I have a model that’s a certain distance of the player and I want to change the size of each of the parts so that they become smaller the closer that the player is to that model.

The problem, however, is that it’s quite taxing if you were to loop through each of these parts for each render frame to update the Size property, especially when you have a model with above ~500 parts.

Of course, I’ve tried the Scale property of the model, but I want each part to retain their initial position, which isn’t the case when you scale the model.

I’ve tried looking in to passing a shared “static” variable as reference to multiple parts so that when you update that single variable it updates for all of the instances that have their reference to it, but unfortunately I couldn’t find anything about that.

Are there any efficient solutions that could help me out? Thanks in advance!

2 Likes

Sounds like a job for parallel Luau

Is setting the size in parallel possible? I thought parallel Luau isn’t allowed to change it.

@aSuspiciousPickle, do you really need to change the size every render frame? Or would it be sufficient to change it maybe ten times per second? Or even just a few times per second?

Hm. You’re right. I can’t believe race conditions escaped me for a moment. BasePart.Size is a read-only property in a desynchronized thread.

The next thought that comes to mind is to only affect the models within a certain range. Do what Minecraft does to circumvent chunk-loading: apply a radial fog/view-distance

Seems like for your case you need to change each part’s size individually. You can do some optimizations like

  1. (as mentioned) you can update only parts that are close/visible to the player
  2. update them only if the change in distance between the model and player is bigger than a certain value, so that it doesn’t loop when player is standing in place + reduce update times when moving (you’ll need to save the distance from the last size update, and add/subtract the player’s moved distance from that point)