i have a part with a text label i want to write on that part how many players are on it
01 | local Players = game:GetService "Players" |
02 | local function getPlayersOnPart(regionPart) |
03 | local pos 1 = regionPart.Position+Vector 3. new( 0 ,. 3 , 0 ) - (regionPart.Size / 2 ) |
04 | local pos 2 = regionPart.Position+Vector 3. new( 0 ,. 3 , 0 ) + (regionPart.Size / 2 ) |
05 | local region = Region 3. new(pos 1 , pos 2 ) |
06 | local partsInRegion = workspace:FindPartsInRegion 3 (region, nil , 1000 ) |
07 | local tab,x = { } , 0 |
08 | for _, 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 |
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:
01 | X = 0 -- since no one is in the server, total players are 0 |
02 | local Text = script.Parent.ScreenGui.TextLabel -- Locate where is the text. If you don't know, take a screenshot |
03 |
04 | game.Players.PlayerAdded:Connect( function () -- when someone joined |
05 | X = X + 1 -- plus 1 |
06 | Text.Text = X -- updates the text whenever someone joined |
07 | end ) |
08 |
09 | game.Players.PlayerRemoving:Connect( function () -- when someone is leaving |
10 | X = X - 1 |
11 | Text.Text = X -- updates the text whenever someone left |
12 | end ) |