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

Issues with player count?

Asked by 9 years ago

My game needs at least two players to begin. My script won't continue even if I have two players in my test server.

while true do

        print("this works") -- this prints over and over again even though I am using the Test feature              with two players
        wait(1)

        contestants = {}

        for _, player in pairs(game.Players:GetPlayers()) do

            local humanoid = player.Character:WaitForChild("Humanoid")

            if humanoid and humanoid.Health > 0 then

                table.insert(contestants, player)

            end

        end

    end

    if #contestants >= 2 then

        print("we have enough") -- this does not print

        break

    end

Thanks in advance :)

0
To add on to the question and this, use >= rather than > as you are saying at least unmiss 337 — 9y

3 answers

Log in to vote
3
Answered by 9 years ago

We can keep track of the quantity of Players by using a Variable and use PlayerAdded and PlayerRemoving event. I'll post any example below. I'l comment you through the script.

local TotalPlayer = 0--Variable that I created to keep track of the Number of players in the game.

game.Players.PlayerAdded:connect(function(Enter)--Function Fires the PlayerAdded event everytime a player joins a game
TotalPlayer = TotalPlayer + 1--If a Player Joins the game then it adds 1 to the TotalPlayerValue
print(TotalPlayer)--Prints the TotalPlayer Value
end)

game.Players.PlayerRemoving:connect(function(Leave)-- Function fires the PlayerRemoving event everytime a player leaves the game
TotalPlayer = TotalPlayer - 1-- Everytime a Player leaves the game it subtracts 1 from TotalPlayer Value
print(TotalPlayer)-- Print the TotalPlayer Value
    end)

wait(1)

if the TotalPlayer > 2 then
print(TotalPlayer.." are in the game")

--Code here

Helpful Links: PlayerAdded PlayerRemoving Variable Parameters

Ad
Log in to vote
0
Answered by
ImageLabel 1541 Moderation Voter
9 years ago

The problem with your code is that you are creating and filling up a table in an infinite loop. Therefore, for every time the loop runs, a new table is created, causing issues with keeping track of what values were already index in what table.

A simple fix would be to create the table contestants outside of the loop.

local players = game:GetService('Players'):GetPlayers()
local contestants = {}

while true do

    for index = 1, #players do
        local player = players[index].Character
        if player and player.Humanoid and player.Humanoid.Health > 0 then

            contestants[#contestants + 1] = players[index]
        end
    end

    if #contestants > 2 then
        break
    end
end
Log in to vote
0
Answered by 9 years ago

This is basically the same as all the others, but let me script it in my way, because why not? Lol.

local contestants = {}
local req = 2 -- number of players needed
repeat wait() until game.Players >= req
for a,b in pairs(game.Players:GetChildren()) do 
    table.insert(contestants,b)
end
-- add your game code here.

or

local contestants = {}
local req = 2 -- number of players needed

while wait() do
    if game.Players.NumPlayers >= req then
        break
    end
end
-- add your game code here.

There's actually a lot of ways to do this. This is just a very easy way of doing it.

0
Thanks everybody! I can't test your things yet, but I have no doubt they work. I'll accept an answer once I am able to use studio. secretassassin3 30 — 9y

Answer this question