So, I am making a round script for a game and when I playtest in studio by myself the value of the status does not change to say “Waiting for more than 1 player”
Script:
--variables
local status = game.ReplicatedStorage.Status
local players = game:GetService("Players")
local roundtime = 120
local intertime = 15
--tables
local ingame = {}
--playercount
local playercount = 0
while task.wait() do
playercount = #players:GetChildren()
end
--main
while task.wait() do
if playercount >= 2 then
else
--this part is not showing when i check the value of the status
status.Value = "Waiting for more than 1 player"
end
end
The easiest way to fix this might be to move the line that updates `playercount` into the main loop. (click for code snippet)
--variables
local status = game.ReplicatedStorage.Status
local players = game:GetService("Players")
local roundtime = 120
local intertime = 15
--tables
local ingame = {}
--playercount
local playercount = 0
--main
while task.wait() do
playercount = #players:GetPlayers()
if playercount >= 2 then
else
--this part is not showing when i check the value of the status
status.Value = "Waiting for more than 1 player"
end
end
You could also use events instead of a loop, which might be slightly more efficient than the above method. (click for code snippet)
--variables
local status = game.ReplicatedStorage.Status
local players = game:GetService("Players")
local roundtime = 120
local intertime = 15
--tables
local ingame = {}
--playercount
local playercount = 0
function updatePlayerCount()
playercount = #players:GetPlayers()
end
players.ChildAdded:Connect(updatePlayerCount) -- fires when something under players is added
players.ChildRemoved:Connect(updatePlayerCount) -- fires when something under players is removed
--main
while task.wait() do
if playercount >= 2 then
else
--this part is not showing when i check the value of the status
status.Value = "Waiting for more than 1 player"
end
end
Also, it’s advised to use Players:GetPlayers() instead of Players:GetChildren() when you specifically want to get Players and not other Instances under the Players object. It works the same way as GetChildren but filters for only Player objects.
Finally, it might be worth noting that you could use Players.PlayerAdded event and Players.PlayerRemoving event, but the PlayerRemoving event only fires right before a player leaves, not after, so the Player object might still exist, so I didn’t want to make anything more confusing.
local Game = game
local Players = Game:GetService("Players")
while true do
repeat
Players.PlayerAdded:Wait()
until #Players:GetPlayers() == 5
--Start round here.
end
Avoid using arbitrary waits when it isn’t necessary to do so.