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

Check if certain amount of players in game?

I have pasted part of my script below. The script is supposed to check if there are two people in the game, and if so then delete a GUI, but if not then it should just keep waiting. HOWEVER, when I play online to test it out, I am the only person in the game but the script will start running as if there were 2 people.

while true do
	if game.Players.NumPlayers >=2 then  --Check if more than or equal to 2 players
	for i, v in pairs(game.Players:GetChildren()) do
    local playergui = v:FindFirstChild("PlayerGui")
    if playergui then
    local somegui = playergui:FindFirstChild("playercheckgui")
    if somegui then
    somegui:Destroy()
	print("Destroyed first GUI in PlayerGui")
	else
	print("Wait")		
			end
		end
	end
end
------
end --(this end is for the while true do where there is more stuff inbetween here)

1 answer

Sorry for this rushed code, not tested but should work?


local Players = game:GetService("Players")
local playerAmount = 0
 
local function onPlayerAdded(player)
     playerAmount = playerAmount + 1
	
	if playerAmount >= 2 then
		--Destroy gui
	end
end
 
Players.PlayerAdded:connect(onPlayerAdded)

1 comment

0

You forgot to account for when players leave the server. :P

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