StringValue value not changing

Hey developers!

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

All help appreciated! Thank you!

1 Like

That is because the first while loop will run forever. Meaning any code below wont run.

I suggest wrapping it inside a spawn function()

1 Like

What is a spawn function()? I am new to scripting.

That’s because you did not add any wait() at the part thats not showing and the loop will restart immediately

1 Like

Do you mean this part?

	if playercount >= 2 then
		
	else

yes add a wait at the end of the line where it changes the status

1 Like

Alright, thank you!

This text will be blurred

You could put it in a separate thread.

Example:

coroutine.wrap(function()
    while wait() do
        -- lalala...
    end
end)()

Also, put this BEFORE the player count checker…

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.

3 Likes
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.