How do I get a list of the players that are touching a part?

So what I wanna do I want to script a rap battle but for that I need to see the players that are in an area(part) so I can choose who is battling against who, how could I get a list of the people that are touching a part(in an area)?

1 Like

You could use BasePart:GetTouchingParts() or WorldRoot:GetPartsInPart()

Do not constantly call these functions though, only call them when a player touches the box, which you can detect with the Touched event

Or you can just use the touched event in itself and keep records with a table

local PlayersInBounds = {}

BasePart.Touched:Connect(function(otherPart)
   if not otherPart.Parent:IsA("Model") or not otherPart.Parent:FindFirstChildOfClass("Humanoid") then return end
   PlayersInBounds[otherPart.Parent.Name] = game.Players:GetPlayerFromCharacter(otherPart.Parent)
end)

Although if you want something more efficient and easy to use, go for ZonePlus like @iiCore_Zanta suggested

2 Likes

You can use ZonePlus to make this

3 Likes
local Zone = require(game.ReplicatedStorage.Zone)
local container = workspace.RapBattle
local zone = Zone.new(container)
local playersinarea = zone:getPlayers()

So that would be all the players that are in the zone?
So I can just check how many players there are and which players there are? How would I check how many players there are with this code and which players are in the zone? Please send me a code sample.