What is the MOST efficient and fastest way to run code

Hey there! So currently I am developing a self driving car that scans its surroundings every .001 second. The current system is working, my car is scanning its surroundings every .001 second, and can adapt to it. However, when players join using a lower end device or slow WiFi, it tends to lag. I can NOT scan for more than .001 second, not even .002, as this would cause delay (yes it can be that bad), currently, my system uses “while true do” and the wait is only .001 to prevent crashing. Are there any better way to make this more efficient?

Use RunService.Heartbeat. It calls the event every physics step which is ideal for checking physical surroundings

1 Like

You can’t actually wait for less than 29ms = 0.029s = 34Hz^-1. Heartbeat should run at up to 60 Hz, so it should be an improvement. That’s still 16.6ms though, so nowhere near your desired 1ms. AFAIK there’s no way to run code faster than 60 Hz, except for running on RenderStepped and asking the player to install an FPS unlocker :stuck_out_tongue:

EDIT: Like Decablocks mentioned the physics only run at that same, fixed rate, so there’s no point checking faster than that because there’s no way the surroundings could have changed due to physics.

1 Like