How can I detect in a script if there is any entity (npc) near the player and which one is the closest

How can I detect in a script if there is any entity (npc) near the player and which one is the closest?

I need to make a system in which even if the player has his back at an enemy you can still press e and release a sphere to go to the enemy

Something like this

You can store the entities inside a folder that will be checked using an in pair loop. Then, you can calculate the magnitude of each npcs and store the closest npc. Afterward, you can send the sphere to the closest npc that is calculated.

Use collectionService to tag object that need to be looped and deduct their positions to workout magnitude and save the one with the samllest distance etc

1 Like