Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

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

Asked by 4 years ago

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

0
Do you have any code for it? Retallack445 75 — 4y
0
i don't OctavianH0310 25 — 4y

2 answers

Log in to vote
0
Answered by 4 years ago
01local Players=game:GetService"Players"
02local function getPlayersOnPart(regionPart)
03local pos1 = regionPart.Position+Vector3.new(0,.3,0) - (regionPart.Size / 2)
04local pos2 = regionPart.Position+Vector3.new(0,.3,0) + (regionPart.Size / 2)
05local region = Region3.new(pos1, pos2)
06local partsInRegion = workspace:FindPartsInRegion3(region, nil, 1000)
07local tab,x={},0
08for _, v in ipairs(partsInRegion) do
09        local char = v.Parent
10        local hum = char and char:FindFirstChildOfClass"Humanoid"
11        local player=hum and Players:GetPlayerFromCharacter(char)
12    if player and not tab[player]then
13            tab[player]=true
14            x+=1
15    end
View all 29 lines...
0
Can you explain it so we can understand the script? Like, the Region3 functions and stuff. Otherwise this would be spoonfeeding Omq_ItzJasmin 666 — 4y
0
He was asking a simple script, why is this off-topic so much and it's not needed. Xapelize 2658 — 4y
0
Deserved a downvote anyways. You aren't really explaining any scripts and you keeps giving-away answers. Xapelize 2658 — 4y
Ad
Log in to vote
0
Answered by
Xapelize 2658 Moderation Voter Community Moderator
4 years ago

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:

01X = 0 -- since no one is in the server, total players are 0
02local Text = script.Parent.ScreenGui.TextLabel -- Locate where is the text. If you don't know, take a screenshot
03 
04game.Players.PlayerAdded:Connect(function() -- when someone joined
05    X = X + 1 -- plus 1
06    Text.Text = X -- updates the text whenever someone joined
07end)
08 
09game.Players.PlayerRemoving:Connect(function() -- when someone is leaving
10    X = X - 1
11    Text.Text = X -- updates the text whenever someone left
12end)

Answer this question