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 3 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 — 3y
0
i don't OctavianH0310 25 — 3y

2 answers

Log in to vote
0
Answered by 3 years ago
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



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 — 3y
0
He was asking a simple script, why is this off-topic so much and it's not needed. Xapelize 2658 — 3y
0
Deserved a downvote anyways. You aren't really explaining any scripts and you keeps giving-away answers. Xapelize 2658 — 3y
Ad
Log in to vote
0
Answered by
Xapelize 2658 Moderation Voter Community Moderator
3 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:

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)

Answer this question