Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

My Intermission/Round Script is stuck. Why is it?

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?

4 comments

0

What exactly do you mean it gets stuck?

0

Even if there is multiple players, It still displays "Waiting for multiple players..." in the status bar.

0

"if #game.Players:GetChildren() > 1 then break", put that inside of the loop it's getting stuck in

0

also use GetPlayers() not GetChildren()

2 answers

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.

3 comments

0

It -still- didn't work.

0

It does work, however the values you game only change values within replicated storage, perhaps you are trying to change a text label?

0

Yes.

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

5 comments

0

That still wouldn't work.

0

Please let me know if this works :D

0

@MCAndRobloxUnited, sure it would.. ?

0

Not for him. If the loop still gets stuck he can always break out of it, but he doesn't know that.

0

-1 suggesting deprecated items "NumPlayers"

This discussion is preserved as it was. New answers, comments, and votes are closed.