How to find the closest player

I’ve made a module to find the closest player within range

here’s the model:
https://www.roblox.com/library/9672243892/FindClosestPlayer

Explanation

so basically let me explain what it does
(explanations are in the comments of the code below)

local Players = game:GetService("Players") --playerservice

-- to make this work you'll need to send what position you're going to search the nearest player from
-- next you'll need to send the max range/radius of how far it'll search
local function findClosestPlayer(position, range) -- requires 2 arguments/parameters
	local closestPlayer -- a placeholder for the closest player
	local closestDistance = range -- automatically sets the closest distance to the range you sent
	for _, player in pairs(Players:GetChildren()) do --loops through each player in Players
		if player.Character then -- if player has character then it continues 
			local distance = player:DistanceFromCharacter(position) -- gets the distance from the position to the player
			if distance <= closestDistance then -- then compares the distance between the player to the position with the closest distance
--if distance from the player is less than closest distance then
				closestDistance = distance  --it will set the closestDistance the distance from the position to the player
				closestPlayer = player -- change closestPlayer to the closest player
			end
		end
	end
	return closestPlayer, closestDistance --returns closestPlayer and closestDistance so you can use them 
end

return findClosestPlayer
Usage

here’s the model:
https://www.roblox.com/library/9672243892/FindClosestPlayer

now you can place it in preferably in replicated storage or server storage

depending on where you put the module
you’ll have to get the corresponding name to it, for example if i put it in replicated storage i’ll have to make a path for it such as

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local findClosetPlayer = require(ReplicatedStorage.FindClosestPlayer)

for the position, you’ll have to make it a vector3 value either from a part position or a given vector 3 position

-- it could like this
local position = workspace.Part.Position
-- or a vector3 value
local position = Vector3.new(0, 100, 0)

now for the range you can just simply make a variable for it such as

local range = 100

now to actually use this thing is by
making 2 variables closestPlayer and closestDistance (if you aren’t going to use the closest distance then you dont have to include it)
equal to the module script function findClosetPlayer
and putting position and the range as the parameters

local closestPlayer, closestDistance = findClosetPlayer(positon,maxRange)

and all together it should look something like
(doesn’t have to be exact and its just an example)

local ServerStorage = game:GetService("ServerStorage")
local findClosetPlayer = require(ServerStorage.FindClosestPlayer)

local positon = Vector3.new(10,10,10)
local maxRange = 100

local closestPlayer = findClosetPlayer(positon,maxRange)
print(closestPlayer)

and depending on your case in scenario, if you’re making an npc to face the nearest player you might have to use a while loop and put the function inside of it such like this

local ServerStorage = game:GetService("ServerStorage")
local findClosetPlayer = require(ServerStorage.FindClosestPlayer)

local maxRange = 1000

while true do
	local closestPlayer = findClosetPlayer(workspace.npc.PrimaryPart.Position,maxRange)
	if closestPlayer then -- just always check if closestPlayer it sometimes might return nil if there's no players within range
		--make npc to face the closestPlayer
	end
	task.wait() -- also your loop might break if you don't have this
end

if you have any questions about this feel free to ask

and special thanks to @ArgonTheory for helping me fixing this

8 Likes