I have no idea how to make this work that's why there is no code here
I hope this works for ya!
function getClosestHumanoid(player, limit) -- limit defined for how far the function will get the closest humanoid in studs local closest, inst = nil, nil local char = player.Character or player.CharacterAdded:Wait() for i, v in pairs(game.Players:GetPlayers()) do -- loop through the available players if v == player then continue end if closest == nil then local character = v.Character if character and character:FindFirstChild('Humanoid') then local magnitude = (char.HumanoidRootPart.Position-character.HumanoidRootPart.Position).Magnitude -- we convert this into magnitude (distance) if limit and magnitude <= limit then closest = magnitude inst = character elseif not limit then closest = magnitude inst = character end else continue end else local character = v.Character local magnitude = (char.HumanoidRootPart.Position-character.HumanoidRootPart.Position).Magnitude if character and character:FindFirstChild('Humanoid') and magnitude < closest then if limit and magnitude <= limit then closest = magnitude inst = character elseif not limit then closest = magnitude inst = character end else continue end end end return inst.Humanoid -- remove .Humanoid if you want to get the character object end
I hope this can help:
-- function to get the closest humanoid to a specific player: function getClosestHumanoid(plr) local char = plr.Character if char then local closest_char = nil local closest_pos = nil -- loops through all the children of workspace: for _,v in pairs(workspace:GetChildren()) do local humanoid = v:FindFirstChild("Humanoid") -- checks if it has a humanoid: if humanoid then local root = v.PrimaryPart -- aka "HumanoidRootPart" -- checks if there is no closest character or the distance between the player and the current humanoid is less than the current closest distance if not closest_char or plr:DistanceFromCharacter(root.Position) < closest_pos then closest_char = v closest_pos = plr:DistanceFromCharacter(root.Position) end end end return closest_char, closest_pos end end
Hope this helped a bit