hello, keeping it simple, i want a script that checks all players (or preferably npcs n’ players) and check if they’re in a crowded place (with most players n’ npcs near them) and then returns the player that is in the MOST crowded area
i need nothing else but the code and an explanation
You’ll need a script that iterates through all players and NPCs, checks their positions, and determines which player is in the most crowded area.
-- Function to count the number of players and NPCs near a given position
local function countNearbyEntities(position, radius)
local count = 0
for _, player in ipairs(game:GetService("Players"):GetPlayers()) do
if player.Character and (player.Character.HumanoidRootPart.Position - position).magnitude <= radius then
count = count + 1
end
end
for _, npc in ipairs(game.Workspace:GetDescendants()) do
if npc:IsA("Model") and npc:FindFirstChild("Humanoid") and not game:GetService('Players'):GetPlayerFromCharacter(npc) then
if (npc.PrimaryPart.Position - position).magnitude <= radius then
count = count + 1
end
end
end
return count
end
-- Function to find the entity in the most crowded area
local function findMostCrowdedEntity()
local mostCrowdedEntity = nil
local maxCount = 0
for _, player in ipairs(game:GetService("Players"):GetPlayers()) do
local count = countNearbyEntities(player.Character.HumanoidRootPart.Position, 50) -- Adjust radius as needed
if count > maxCount then
maxCount = count
mostCrowdedEntity = player
end
end
for _, npc in ipairs(game.Workspace:GetDescendants()) do
if npc:IsA("Model") and npc:FindFirstChild("Humanoid") and not game:GetService('Players'):GetPlayerFromCharacter(npc) then
local count = countNearbyEntities(npc.PrimaryPart.Position, 50) -- Adjust radius as needed
if count > maxCount then
maxCount = count
mostCrowdedEntity = npc
end
end
end
return mostCrowdedEntity
end
-- Example usage
local mostCrowdedEntity = findMostCrowdedEntity()
if mostCrowdedEntity then
print("Most crowded entity:", mostCrowdedEntity.Name)
else
print("No entities found.")
end