Efficiency in checking for position / magnitude?
So I'm trying to make a basic NPC-like dialog GUI appear when a player walks near a certain part. Only problem is, I feel as if my code is somewhat inefficient. I use a while loop and magnitude to compare the player's torso position, to the target part. Here's a brief example of what it looks like:
02 | local players = game:GetService( "Players" ) |
05 | local client = players.LocalPlayer |
06 | local mouse = client:GetMouse() |
07 | local char = client.Character or client.CharacterAdded:wait() |
08 | local torso = char:WaitForChild( "Torso" ) |
11 | local npc = workspace:WaitForChild( "NpcPart" ) |
15 | if (torso.Position - npc.Position).Magnitude < 10 then |
16 | print ( "Player is in range" ) |
So this does work, however If i wanted a really vast amount of NPC objects, I feel like this could get messy and inefficient really quick. I've also heard about Region3, but I'm not sure if that would be a good choice either.
If someone knows a bit more about this than me and is willing to share, I'd appreciate it!