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

How do I check the amount of players inside of a game using a script?

I am trying to make a GUI only appear to the player online when there is 1 player online. Any suggestions?

1 comment

0

The main reason why I’m asking this is because when I search up “How to check the amount of players inside of a game Roblox” on YouTube, all that shows up are tutorials on checking the last time a specific player played lol.

2 answers

✓ Accepted answer

Hello, the easiest way is by getting the player list and then getting the size of elements inside the table. To get the player list, you use game.Players:GetPlayers(), that will return a table with the current players, so we can get the number of elements inside that table by doing #game.Players:GetPlayers(), that will be the number of players currently on the server

1 comment

0

Thanks!

Hi there. What you can do, is this:

local Players = game:GetService("Players") --gets players
local PlayerCount = #Players:GetPlayers() --gets the amount of players
local GUI = script.Parent --reference variable for your gui, index it to wherever your GUI is

GUI.Visible = false --makes the gui disappear

Players.PlayerAdded:Connect(function() --if a player gets added, 
    PlayerCount = PlayerCount + 1 --increase the playercount by 1
end)

Players.PlayerRemoving:Connect(function() --if a player gets removed,
    PlayerCount = PlayerCount - 1 --decrease the playercount by 1
end)

if PlayerCount == 1 then --if the playercount is equal to one, we can do this because if there's only one player in your game, the gui would only be visible to the one player in your game
	GUI.Visible = true --make the GUI visible
end

if PlayerCount >= 1 then --if the playercount is higher than 1 then
	GUI.Visible = false --make the GUI disappear
end 

I'm pretty sure this will work. Thanks, and have a good day.

2 comments

0

Thanks, let me test it...

0

I’m pretty sure that you need to put a “while,do” statement for the if statements, but otherwise, the script works.

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