How would I get the number of players inside a region3?
Get number of players inside a region3?
1 answer
local Players = game:GetService("Players")
local myRegion = Region3.new(Vector3.new(0, 0, 0), Vector3.new(50, 50, 50))
local partsInRegion = workspace:FindPartsInRegion3(myRegion, nil, math.huge)
local characters = {}
for i, v in pairs(partsInRegion) do
local player = Players:GetPlayerFromCharacter(v.Parent)
if player and not characters[player] then
characters[player] = true
end
end
print("Number of players in Region: "..#characters)
I would do it like this, sort of, this wouldn't really work since it only checks partsInRegion when the game first start. But the princip is the same.
You should read about this FindPartsInRegion3
We begin with creating the Region3 and then we move on to getting the parts inside the region, which you should do in the loop or function you'll use. We also have an empy table to fill with the players inside the region. We then loop through all the parts from partsInRegion and checks if they are parts of a player, if so, we check if the player is already inside our table and if not we add them to it.
The last thing we do is print out how many players we found, and this you could do only if the amount changes if you'd want.
Hope it helped.
This discussion is preserved as it was. New answers, comments, and votes are closed.
