Skip to content

Instantly share code, notes, and snippets.

-- Define the maximum distance to consider for finding the closest player
local maxDistance = 50 -- Adjust as needed
-- Function to find the closest player within the specified distance
local function findClosestPlayer()
local closestPlayer = nil
local closestDistance = math.huge -- Initialize with a very large number
for _, player in ipairs(game.Players:GetPlayers()) do
-- Ensure the player is not yourself and their character exists