✓ Accepted answer
I don't know exactly, but I saw it being done. There is a thingy called magnitude that returns the distance from point A to point B, so you could have a script that is similar to the following one:
local target = nil
local dis = 20
while true do wait(1)
local cpos = script.Parent.Position
local nearest = math.huge()
local players = game.Players:GetChildren()
for _, player in ipairs(players) do
local chara = player.Character
if chara ~= nil then
local torso = chara:FindFirstChild('Torso')
if torso == nil then torso = chara:FindFirstChild('UpperTorso') end
local mag = (cpos - torso.Position).magnitude
if mag < nearest and mag < dis then
nearest = mag
target = player
end
end
end
end
I wrote this really quickly, so I didn't test it. (May not work)
Try altering this
Accept if helped