RobloxScripterAI
Discord ↗
Link Copied to Clipboard
×
"create a function that loops through the players and returns the closest player"
-- server script function getClosestPlayer(position) local closestPlayer = nil local closestDistance = math.huge for _, player in pairs(game.Players:GetPlayers()) do if player.Character and player.Character:FindFirstChild("HumanoidRootPart") then local distance = (player.Character.HumanoidRootPart.Position - position).magnitude if distance < closestDistance then closestDistance = distance closestPlayer = player end end end return closestPlayer end