Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How can i count the number of the players on a part?

i have a part with a text label i want to write on that part how many players are on it

2 comments

0

Do you have any code for it?

0

i don't

2 answers

✓ Accepted answer
local Players=game:GetService"Players"
local function getPlayersOnPart(regionPart)
local pos1 = regionPart.Position+Vector3.new(0,.3,0) - (regionPart.Size / 2)
local pos2 = regionPart.Position+Vector3.new(0,.3,0) + (regionPart.Size / 2)
local region = Region3.new(pos1, pos2)
local partsInRegion = workspace:FindPartsInRegion3(region, nil, 1000)
local tab,x={},0
for _, v in ipairs(partsInRegion) do
	    local char = v.Parent
        local hum = char and char:FindFirstChildOfClass"Humanoid"
        local player=hum and Players:GetPlayerFromCharacter(char)
	if player and not tab[player]then
			tab[player]=true
			x+=1
	end
	end
	for _,v in ipairs(Players:GetPlayers())do
		tab[v]=not not tab[v]
	end
return tab,x
end

while wait(2)do
	local players,num=getPlayersOnPart(workspace.Baseplate)
	print("number of the players on a part: "..num)
for player,isOnPart in pairs(players)do
	print(player,isOnPart)
	end
	end



3 comments

0

Can you explain it so we can understand the script? Like, the Region3 functions and stuff. Otherwise this would be spoonfeeding

0

He was asking a simple script, why is this off-topic so much and it's not needed.

0

Deserved a downvote anyways. You aren't really explaining any scripts and you keeps giving-away answers.

First, make a simple X variable. This will be explained later.

Next, you can use a :PlayerAdded() function, so when someone is added, X will plus 1.

If a person leave, use the :PlayerRemoving function, so when someone leave, X will minus 1.

The script will look like this:

X = 0 -- since no one is in the server, total players are 0
local Text = script.Parent.ScreenGui.TextLabel -- Locate where is the text. If you don't know, take a screenshot

game.Players.PlayerAdded:Connect(function() -- when someone joined
	X = X + 1 -- plus 1
	Text.Text = X -- updates the text whenever someone joined
end)

game.Players.PlayerRemoving:Connect(function() -- when someone is leaving
	X = X - 1
	Text.Text = X -- updates the text whenever someone left
end)

This discussion is preserved as it was. New answers, comments, and votes are closed.