I’m pretty new at scripting and I’m trying to make a script that waits for players before the game starts and it shows it on top of the screen with a GUI. I’ve got the part where if two players join, the number goes down and it would say “1 more player needed to start” My problem is that when one of the players leaves the game, the number doesn’t go back up. It would still say “1 more player needed to start” and not “2 more players needed to start” as I would like. Thank you!
Here’s the script…
["Players needed to start game"] = 3;
};
Waiting = game.StarterGui.WaitingForPlayer.MainFrame.TextLabel
if game.Players.NumPlayers <= controltable["Players needed to start game"] then
repeat wait() Waiting.Text = controltable["Players needed to start game"]-game.Players.NumPlayers.." more players needed to start" until game.Players.NumPlayers >= controltable["Players needed to start game"]
Waiting.Text = "Players acquired!"
wait(1)
end ```
I think something like this would work. Every time a player joins or leaves the game, It will fire the “CheckPlayers” function to check the number of players on the server. If It’s greater than 3, you can start the game otherwise display other stuff. Let me know if this is what you wanted.
local Players = game:GetService("Players")
local function CheckPlayers ()
local NumPlayers = #Players:GetPlayers()
if NumPlayers >= 3 then
--Display "Game has been Started"
elseif NumPlayers == 2 then
--Display "1 more player needed to start"
elseif NumPlayers == 1 then
--Display "2 more player needed to start"
end
end
Players.PlayerAdded:Connect(CheckPlayers)
Players.PlayerRemoving:Connect(CheckPlayers)
Basically, when a player joins or leaves, it sets the GUI text for him only, you need to change it for everyone else too. You can either use RemoteEvents and FireAllClients(), otherwise, just run a loop for all the players on the server.
for i, player in pairs(Players:GetPlayers()) do
player.StarterGui.WaitingForPlayer.MainFrame.TextLabel = "TextHere"
end