Inquiry regarding loops

Hello developers, I am currently working on a function which is going to fire a ray from a player’s torso shooting down, now obviously the character moves so I need to make a loop that updates the ray’s position.

Now that you have the context, here is my question; my intention is to make the rays update constantly for several players. Would it be better for performance to have several loops updating the individual rays or to have one big loop that iterates through all the rays and updates them?

I personally believe that one large loop it would be much better for optimization if done correctly. Overall you’re just reducing the amount of instances.

I also recommend to cache when the last time you casted the ray in a table, this way you can spread out the timing between all the players rather than raycast all at once.

1 Like

I like that idea, however the ray’s will all start casting at the same time so it wouldn’t make much of a difference I think.

I will most likely do one big loop though

There is a considerable difference between casting all threats at once and spreading the casts.

Imagine you had 200 players, every 0.5 seconds you cast 200 rays all at once, this can cause noticeable lag spikes (depending on your situation and what you’re doing with the results), whereas I could have 200 players casting rays at 0.5 second interval at their own rate. This mean that there will be an offset for all the players if some join later. If one joins 0.3 seconds have another player 0.5 seconds ago, that player would have an offset. This is a tactic to spread everything out while keeping a certain rate.

Now obviously if you must cast all at once because of how your game works then there isn’t much of an option.

I guess I will give you some more context on what this is being used for.

I am making a more advanced version of spleef and we all know .Touched is unreliable and I the parts are slanted so I am not sure how to use Region3 when the parts are angled so I figured I would cast a ray from each player to figure out if they stepped on the pad.

It would be best to avoid looping, it causes much lag.

Try using Humanoid.FloorMaterial

Oo that is a smart idea, is it reliable though?

It is 100% reliable unlike the .Touched.

As an example, in some games you hear the walking sounds change when you walk on different materials. This is utilized by Humanoid.FloorMaterial.

2 Likes

Oh okay I will try it out then, tysm

1 Like