What is the fastest loop?

I have a question, is this seen as expensive?
It is repositioning every bone in a custom model at 10’s of times per second, knowing that its 10’s of times and not 100’s of times gives me the idea that it is expensive

It looks expensive but you can open script activity monitor to see if it uses alot of resources.

Your wait function does not need a 1/9999999999 fraction. Just put wait(). Miniumum time is 0.03 seconds.

1 Like

I might have an idea to combat this, is it possible if I make the loop fire a function and then the function does it, or does it still carry the same amount of data

Unless you open a new thread using spawn() then it will still run the code in the thread or the loop.

My while do statement is in a spawn function so I guess this combats it?

There is no difference between a while loop and a for loop’s speed.

Using while true do hangs the main thread forever unless there’s a breaking condition. You can achieve the same by doing for i = 1, 100000000000. It will hang like the while loop, except it will exit at some point, since it can only loop for so long.

There’s no “Fastest loop”, it just depends on which loop you need and what is being processed in that loop.

If you want to run code as early as possible. RunService.Stepped would be your best choice. As it’s fired before the physics simulation. Which allows you to do any processing on objects before their positions and physics properties are updated.

1 Like

Well that doesnt matter because thats just makes the loop into a different thread than main script. using spawn in every iteration of the loop.

Thats the reason why people use Wait()s for loops because they run so fast.

No. wait() should be avoided as it relatively expensive in some cases. You can write a custom wait() and it could be even faster.

sorry for causing alot of trouble, im not an expert in loop speed and physics simulation :slightly_frowning_face:

Oh don’t worry, no problem with you.

So it seems runservice.Stepped is working slightly better. I also simplified the code so I guess thats a start

Wait() may not always yield the specified time. It depends on the machine that’s processing the function.

Creating a custom wait with RunService.Stepped would be a more accurate way of waiting a certain amount of seconds, as deltatime can be used to increment the timer. (The time between the last .Stepped call and the current call.)

2 Likes

I agree with you. Smart guy^^^^^^^

2 Likes

I have a question, does setting a parts network owner allows u to modify it on the client and it shows the same on the server? If not I will stick to using Stepped on server

Setting the NetworkOwner only specifies which machine will process the object’s physics. This was implemented to take off some of the server load.

You may notice in some games, when a projectile passes by certain a player, it may lag for a bit or seem to move differently. That is caused due to the server relying on the closest client’s machine to calculate the physics for that object as it passes by. Then, the server replicates the results.

Setting the NetworkOwner to nil indicates that the server will always process the object’s physics. You’d want to keep the server processing an AI’s movement for example, as it will be consistant. If not, then the AI will start moving in weird ways when it gets close to clients. As their machines will take over the physic’s processing.

So if that’s the use, then it is perfectly fine using Network ownership to position parts and everyone would see it move in the server also, If so this is exactly what I need

Well, not necessarily. You’d still need to replicate any changes to the server if done by a LocalScript (On the client).

Other than that, the client’s machine will only calculate the object’s physics. Just as the server would. The only difference is the server does not have to do all the work. It just displays the results that are returned by the client’s machine.

I understand. I will try to experiment with this to see if it works as good as I think it does