How can I reliably check the distance between a player and an object consistently

Question:

How can I constantly check the distance between two parts without slowing down the server too much?

Goal:

I am hoping to make a system that checks whenever a player is within a certain range of a part.

Attempts:

  • Runservice (fear that it may slow down the server too much.)
  • Touched: Unreliable.
  • Raycasting: Unable to do until the distance is checked.

Runservice lags if the script has an expensive function, and is the best choice for it to check consistently.

In addition to what @Crictoli said, you could also handle it locally depending on what it is. If it’s a UI or some form of interaction system, you should handle it locally instead of constantly checking on the server. This will also cause it to be much faster as it doesn’t take into account ping or anything.

Your best attempt is to use RunService, since this not relies on rendering you should not use RenderStepped. (Forgot you said server, but still RunService is what I recommend)

No there is Render and there is Simulation, the server do not render, it only simulates.

1 Like

You should use Magnitude and you should only fire this function when the player really moves.

Check if the player has changed position(or the part) and then run the magnitude script again.

(playerrootpart.Position -part.Position).Magnitude

2 Likes

You don’t have to worry. Minus is not an extremely expensive function neither is .Magnitude

The answer is above this one.

local distance = (position1 - position2).magnitude

This is a expensive function. Internally it uses square root which is way more expensive than things like plus or minus.

Run this script only when you need to, to prevent lag.

Thats not true. magnitude uses things like square roots and takes alot of work to calculate

In the grand scheme of things there are more expensive things.

Sure. But it’s not recommended to run this function alot because it takes a bit to run.

I believe that in the end, these two would be running rapidly as either of the two parts would always be moving. If I did a .wait(), there’d be a time period where the script WOULDN’T work if, for example, the player and the part touched.

when I say these two I mean runservice and that.

True. I had forgotten to mention, however, that the said part would also be moving as well.

You won’t need to worry about performance of distance checks unless you’re literally doing thousands of them per frame. Don’t stress, go make your game :slight_smile:

Not really. A slower server leads to a less fun experience.

Roblox servers can easily handle doing hundreds of distance checks every single frame. This is easy to test. My shipped games make heavy use of this, and so do many others. Don’t give bad advise.

2 Likes

Bad advise? your telling people to never care about optimization and that you shouldn’t care.

The reality is you can do what the OP asked for roughly 1,000,000 times per second, or about 16,000 times a frame (60fps) on a roblox server. I have benchmarked it.

You’re making the situation sound much worse that in is by telling them they need to worry about optimizing that.

2 Likes