Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Check if certain amount of players in game?

Asked by
Seyfert 90
8 years ago

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

Log in to vote
0
Answered by 8 years ago

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)
0
You forgot to account for when players leave the server. :P TheeDeathCaster 2368 — 8y
Ad

Answer this question