Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Is there a more efficient way to find the distance from a player to an object?

From what I've heard, while true loops are quite intensive.

Would there be a better way of saying this:

while true do
	wait(0.1)
	if (Player.Character.Position - Object.Position).Magnitude < 3 then
		Object:SetNetworkOwner(Player)
	end
end


Any help is appreciated

1 comment

0

I couldn't think of any alternatives but using RunService to replace the loop.

1 answer

✓ Accepted answer

Simple: There is no alternative unless you want to try reinventing the wheel (which is stupid).

What purpose do you seek for using anything but Magnitude to find distance? Magnitude is really the easiest option for doing it. Obviously you can do a lot of math to reinvent Magnitude, but that is completely unnecessary.

If you're talking about a player specifically, you can use DistanceFromCharacter(), but that is still exactly like Magnitude. Just use Magnitude and don't overthink about distance calculation.

Or, if you seek a loop alternative, RunService works, but it's client-sided.

This discussion is preserved as it was. New answers, comments, and votes are closed.