i have a part with a text label i want to write on that part how many players are on it
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
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)