My Intermission/Round script does run but it gets stuck. Here's the code:
local ReplicatedStorage = game:GetService("ReplicatedStorage") local Status = "" game.ReplicatedStorage.Status.Value = Status local Seconds = 0 local DisplayText = "" game.ReplicatedStorage.DisplayText.Value = DisplayText while #game.Players:GetChildren() <= 1 do Status = "Waiting" game.ReplicatedStorage.Status.Value = Status DisplayText = "Waiting for multiple players..." game.ReplicatedStorage.DisplayText.Value = DisplayText wait(1) end while #game.Players:GetChildren() > 1 do Status = "Intermission" game.ReplicatedStorage.Status.Value = Status Seconds = 15 DisplayText = "Break time!" game.ReplicatedStorage.DisplayText.Value = DisplayText repeat Seconds = Seconds - 1 DisplayText = "Break time will end in "..tostring(Seconds).." seconds..." game.ReplicatedStorage.DisplayText.Value = DisplayText wait(1) until Status == "Intermission" and Seconds == 0 Status = "Round" game.ReplicatedStorage.Status.Value = Status Seconds = 60 DisplayText = "Round time!" game.ReplicatedStorage.DisplayText.Value = DisplayText repeat Seconds = Seconds - 1 DisplayText = "Round will end in "..tostring(Seconds).." seconds..." game.ReplicatedStorage.DisplayText.Value = DisplayText wait(1) until Status == "Round" and Seconds == 0 end
How can I fix it?
GENERAL PRACTICE
Since you pre-defined ReplicatedStorage, there is no need to continue to use "game.ReplicatedStorage" when you have a variable.
Use :GetPlayers()
rather than :GetChildren()
, this is important since if a child other than a player is added to the Players Service, then your round will be able to start with only one player in the game.
You don't need to use tostring()
on the number "Seconds", it is already usable as part of the string without that additional check.
Use :WaitForChild()
to make sure that the two values in ReplicatedStorage exist, rather than assuming they do automatically.
You can place the entire round script into a single while-loop, rather than two distinct ones, and continue to check if the amount of players is less than or equal to one (else, the round begins).
REVISED SERVER SCRIPT
local ReplicatedStorage = game:GetService("ReplicatedStorage") local S = ReplicatedStorage:WaitForChild("Status") local D = ReplicatedStorage:WaitForChild("DisplayText") local Status = "" local DisplayText = "" local Seconds = 0 S.Value = Status D.Value = DisplayText while true do if #(game:GetService("Players"):GetPlayers()) <= 1 then Status = "Waiting" DisplayText = "Waiting for multiple players..." S.Value = Status D.Value = DisplayText wait(1) elseif #(game:GetService("Players"):GetPlayers()) > 1 then Status = "Intermission" Seconds = 15 DisplayText = "Break time!" S.Value = Status D.Value = DisplayText repeat Seconds = Seconds - 1 DisplayText = "Break time will end in "..Seconds.." seconds..." D.Value = DisplayText wait(1) until Status == "Intermission" and Seconds == 0 Status = "Round" DisplayText = "Round time!" Seconds = 60 S.Value = Status D.Value = DisplayText repeat Seconds = Seconds - 1 DisplayText = "Round will end in "..Seconds.." seconds..." D.Value = DisplayText wait(1) until Status == "Round" and Seconds == 0 end end
Any additional comments or questions I will be happy to respond to.
Additionally, if need be, you can use the keyword break
on a loop to force it to stop, as stated in the comments of this question.
Hello, there is no need to use while #game.Players:GetChildren () <1 etc..
There is already a built in thing you can use, try this
local ReplicatedStorage = game:GetService("ReplicatedStorage") local Status = "" game.ReplicatedStorage.Status.Value = Status local Seconds = 0 local DisplayText = "" game.ReplicatedStorage.DisplayText.Value = DisplayText while game.Players.NumPlayers <= 1 do Status = "Waiting" game.ReplicatedStorage.Status.Value = Status DisplayText = "Waiting for multiple players..." game.ReplicatedStorage.DisplayText.Value = DisplayText wait(1) end while game.Players.NumPlayers > 1 do Status = "Intermission" game.ReplicatedStorage.Status.Value = Status Seconds = 15 DisplayText = "Break time!" game.ReplicatedStorage.DisplayText.Value = DisplayText repeat Seconds = Seconds - 1 DisplayText = "Break time will end in "..tostring(Seconds).." seconds..." game.ReplicatedStorage.DisplayText.Value = DisplayText wait(1) until Status == "Intermission" and Seconds == 0 Status = "Round" game.ReplicatedStorage.Status.Value = Status Seconds = 60 DisplayText = "Round time!" game.ReplicatedStorage.DisplayText.Value = DisplayText repeat Seconds = Seconds - 1 DisplayText = "Round will end in "..tostring(Seconds).." seconds..." game.ReplicatedStorage.DisplayText.Value = DisplayText wait(1) until Status == "Round" and Seconds == 0 end