Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 5 years ago

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?

0
What exactly do you mean it gets stuck? Igoralexeymarengobr 365 — 5y
0
Even if there is multiple players, It still displays "Waiting for multiple players..." in the status bar. BleepoBleapo 2 — 5y
0
"if #game.Players:GetChildren() > 1 then break", put that inside of the loop it's getting stuck in DeceptiveCaster 3761 — 5y
0
also use GetPlayers() not GetChildren() DeceptiveCaster 3761 — 5y

2 answers

Log in to vote
0
Answered by 5 years ago
Edited by User#24403 5 years ago

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.

0
It -still- didn't work. BleepoBleapo 2 — 5y
0
It does work, however the values you game only change values within replicated storage, perhaps you are trying to change a text label? SerpentineKing 3885 — 5y
0
Yes. BleepoBleapo 2 — 5y
Ad
Log in to vote
-1
Answered by 5 years ago

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

0
That still wouldn't work. DeceptiveCaster 3761 — 5y
0
Please let me know if this works :D AspectType 151 — 5y
0
@MCAndRobloxUnited, sure it would.. ? AspectType 151 — 5y
0
Not for him. If the loop still gets stuck he can always break out of it, but he doesn't know that. DeceptiveCaster 3761 — 5y
0
-1 suggesting deprecated items "NumPlayers" User#24403 69 — 5y

Answer this question