How do I count all the players inside a part?

I am trying to make an Ayes / Noes voting system with two different lobbies for each vote. - A screen updates based on how many are in each

I have tried this but it counts all body parrts so once person is 18 votes aha. However I am stuck on how to fix this as I am not great in scription. Any help is appreciated!

Thank you all for your suggestions I have now used Zone V2 to get it to work.

local numTouchingParts = 0
local function onTouch(part)
	print("Touch started: " .. part.Parent.Name)
	numTouchingParts = numTouchingParts + 1
local function onTouchEnded(part)
	print("Touch ended: " .. part.Parent.Name)
	numTouchingParts = numTouchingParts - 1
1 Like
local function getPlayersTouchingPart(part)
	local objects = part:GetTouchingParts()
	local players = {}
	local playersCountable = {}
	for _, basePart in pairs(objects) do
		local player = game.Players:GetPlayerFromCharacter(basePart.Parent)
		if (player and players[player] == nil) then
			players[player] = true
			table.insert(playersCountable, player)
	return playersCountable

local playersTouching = getPlayersTouchingPart(workspace.Part)
print(("There are %d players touching the part"):format(#playersTouching))

It prints the players touching at the start of the game, how do I get it to update a text when someone walks into it? image

1 Like

Instead of using Touched events and whatnot, why not try using Region3?

If you don’t know what Regions/Region3 is, think of it as a certain area that you can create using a part or a declared area in your 3D space, I wont give a full tutorial on Region3 but there are plenty of useful resources out there!

I would highly recommend ZonePlus v2 by @ForeverHD
It’s very easy to learn and it has very useful events such as detecting when a player enters the zone or when the player leaves the zone. I would recommend learning Region3 so you really understand how this module works. The documentation is linked below :slightly_smiling_face:

The DevForum post can be found here


You could store the player inside of a table when they enter, and if the player already is in the table when onTouch is called, don’t increase the vote count. When onTouchedEnded is called, simply remove that player from the table.

Before storing it in the table, you have to get the player from that part. To do that, you can use part.Parent and FindFirstChild for the humanoid.

Just fire the function when the Touched event happens.

Thank you so much! This will be useful for so much else in the future.

1 Like