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

Issue With Waiting Players Script? (SOLVED)

Asked by
Bman8765 270 Moderation Voter
10 years ago

The following is a code from a localscript called PlayerWaiter, it's main purpose is to wait until there are at least 2 players online and then start the game. My problem is only the second player that joins see the countdown. For example, one player joins a new game and is waiting, another player joins and now the countdown begins on the 2nd players screen. It does not appear on the 1st players screen though (if that is too confusing just ask me and I'll try to explain it better), anyways here is the code:

----------------------------------------------------------------------------------------------------
repeat wait() until game.Players.LocalPlayer.Character
print("PlayerWaiter script loading variables")
----------------------------------------------------------------------------------------------------
--Insert VARIABLES Below
----------------------------------------------------------------------------------------------------
players = #game.Players:GetPlayers()
PlayerNum = script.Parent.WaitingPlayers
gamestarted = false
----------------------------------------------------------------------------------------------------
print("Playerwaiter script working")
----------------------------------------------------------------------------------------------------

repeat wait()
    PlayerNum.Text = "Waiting for players."
    wait(.5)
    PlayerNum.Text = "Waiting for players.."
    wait(.5)
    PlayerNum.Text = "Waiting for players..."
    wait(1)
until players >= 2

repeat wait()
    if players >= 2 then
        PlayerNum.Text = "Starting in 10"
        wait(1)
        PlayerNum.Text = "Starting in 9"
        wait(1)
        PlayerNum.Text = "Starting in 8"
        wait(1)
        PlayerNum.Text = "Starting in 7"
        wait(1)
        PlayerNum.Text = "Starting in 6"
        wait(1)
        PlayerNum.Text = "Starting in 5"
        wait(1)
        PlayerNum.Text = "Starting in 4"
        wait(1)
        PlayerNum.Text = "Starting in 3"
        wait(1)
        PlayerNum.Text = "Starting in 2"
        wait(1)
        PlayerNum.Text = "Starting in 1"
        wait(1)
        PlayerNum.Text = "Starting Match"
    end
until gamestarted == true
0
So how do I go about fixing this? Bman8765 270 — 10y

2 answers

Log in to vote
1
Answered by 10 years ago

The loop checks the value "players", however the value itself is defined before the loop - as in, it's defined once it is executed. That means the first player will have a value equal to 1 all the time, and the 2nd player will have a value equal to 2 (or however many players are in the game as they join).

To fix this, you simply either update the variable every time the loop runs (1), or checks the actual # of players rather than checking the variable in the loop (2).

I also shortened the script down for you, by using loops. ;)

METHOD 1:

----------------------------------------------------------------------------------------------------
repeat wait() until game.Players.LocalPlayer.Character
print("PlayerWaiter script loading variables")
----------------------------------------------------------------------------------------------------
--Insert VARIABLES Below
----------------------------------------------------------------------------------------------------
players = #game.Players:GetPlayers()
PlayerNum = script.Parent.WaitingPlayers
gamestarted = false
----------------------------------------------------------------------------------------------------
print("Playerwaiter script working")
----------------------------------------------------------------------------------------------------

repeat wait()
        PlayerNum.Text = "Waiting for players."
        wait(.5)
        PlayerNum.Text = "Waiting for players.."
        wait(.5)
     PlayerNum.Text = "Waiting for players..."
        wait(1)
    players = #game.Players:GetPlayers()
until players >= 2

repeat wait()
    if #game.Players:GetPlayers() >= 2 then
        -- Starting at 10, counting to 1, we have to increase by -1
        for t = 10, 1, -1 do
            -- t is a changing variable here, as seen above
            PlayerNum.Text = "Starting in "..t 
            wait(1)
        end
        -- The final text after the countdown is done
        PlayerNum.Text = "Starting Match" 
    end
until gamestarted == true

METHOD 2:

----------------------------------------------------------------------------------------------------
repeat wait() until game.Players.LocalPlayer.Character
print("PlayerWaiter script loading variables")
----------------------------------------------------------------------------------------------------
--Insert VARIABLES Below
----------------------------------------------------------------------------------------------------
PlayerNum = script.Parent.WaitingPlayers
gamestarted = false
----------------------------------------------------------------------------------------------------
print("Playerwaiter script working")
----------------------------------------------------------------------------------------------------

repeat wait()
        PlayerNum.Text = "Waiting for players."
        wait(.5)
        PlayerNum.Text = "Waiting for players.."
        wait(.5)
     PlayerNum.Text = "Waiting for players..."
        wait(1)
until #game.Players:GetPlayers() >= 2

repeat wait()
    if #game.Players:GetPlayers() >= 2 then
        -- Starting at 10, counting to 1, we have to increase by -1
        for t = 10, 1, -1 do
            -- t is a changing variable here, as seen above
            PlayerNum.Text = "Starting in "..t 
            wait(1)
        end
        -- The final text after the countdown is done
        PlayerNum.Text = "Starting Match" 
    end
until gamestarted == true
0
Thank you so much, I literally just figured it out right before you posted it but your answer still helped. Bman8765 270 — 10y
0
Awesome! I edited the 2nd method just a few seconds ago, I had forgotten to replace the players-variable with #game.Players:GetPlayers(). But yeah, you got the point. Ravenshield 180 — 10y
Ad
Log in to vote
0
Answered by
Ywis 5
10 years ago

Maybe the first person can't see the script as it has run before he the stings had loaded,

My answer to this is try loading the first player in to the game then reset the player. It might then show the message to him.

Answer this question