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

Why does the script skip the intermission if it goes from 2 to 1 players?

Asked by 6 years ago
Edited 6 years ago

I've been trying to make a minigames script for a while now, but I can't fix all of the errors for the life of me. My most recent error is that if the game goes from having two players to one player during the intermission, it skips it completely and goes into the round. Thanks in advance for finding the error.

--// %Script by cmgtotalyawesome%

--// Variables

MapStorage = workspace.MapStorage
IntermissionTime = 15
Players = game.Players:GetChildren()
_G.RoundInProgress = false
NumPlayers = game.ReplicatedStorage.NumPlayers
NumPlayers.Value = 0
GameTime = 90

game.Players.PlayerAdded:connect(function(plr)
    NumPlayers.Value = NumPlayers.Value + 1
end)

game.Players.PlayerRemoving:connect(function(plr)
    NumPlayers.Value = NumPlayers.Value - 1
end)
--// Intermission/Gameplay

while wait(1) do
    if not _G.RoundInProgress and NumPlayers.Value >= 2 then
        for i = IntermissionTime, 0, -1 do
            if NumPlayers.Value < 2 then break end
            game.ReplicatedStorage.StatusValue.Value = "Intermission: ".. i
            wait(1)
        end

            local maps = game.ServerStorage.Maps:GetChildren()
            local chosenmap = maps[math.random(1, #maps)]
            local map = game.ServerStorage.Maps[chosenmap.Name]:Clone()
            map.Parent = workspace.MapStorage
            _G.RoundInProgress = true

            for i, v in pairs(game.Players:GetPlayers()) do
                local a = v.Name
                local player = Instance.new("Model", game.ReplicatedStorage.Players)
                player.Name = a
                local Spawns = chosenmap.Spawns:GetChildren()
                workspace[a].Torso.CFrame = CFrame.new(MapStorage[chosenmap.Name].Spawns[math.random(1, #Spawns)].Position)
                if chosenmap.Name == "TeamKnockOff" or chosenmap.Name == "KnockOff" then
                windforce = game.ServerStorage.Windforce:Clone()
                windforce.Parent = v.Backpack
                wait()
                end 
            end

            game.ReplicatedStorage.StatusValue.Value = "Round In Progress"
            for i = GameTime, 0, -1 do
                if _G.RoundInProgress == false then break end
                wait(1)
                game.ReplicatedStorage.StatusValue.Value = "Round in progress, ".. i.. " seconds left"
            end
            workspace.MapStorage:ClearAllChildren()
            _G.RoundInProgress = false
    end
end


1
Instead of using a value in RS to represent the amount of players in a game, just use "#game.Players:GetPlayers()". Before starting the round, check one more time, just in case. Julian_Orteil 48 — 6y

2 answers

Log in to vote
0
Answered by
RayCurse 1518 Moderation Voter
6 years ago
Edited 6 years ago

The break statement inside your for loop will break out of the loop and continue setting up the gameplay regardless of the number of players. You could add another if statement after the for loop that checks if there are enough players.

while wait(1) do
    if not _G.RoundInProgress and NumPlayers.Value >= 2 then
        for i = IntermissionTime, 0, -1 do
            if NumPlayers.Value < 2 then break end
            game.ReplicatedStorage.StatusValue.Value = "Intermission: ".. i
            wait(1)
        end
    end
    if NumPlayers.Value >= 2 then
        --Set up gameplay
    end
end
0
tysm, I've been trying to figure that out for a few hours now... cmgtotalyawesome 1418 — 6y
Ad
Log in to vote
0
Answered by
UgOsMiLy 1074 Moderation Voter
6 years ago
Edited 6 years ago

On line 25. You put:

if NumPlayers.Value < 2 then break end

"break" breaks the loop, in other words, it will stop the "for" loop and immediately continue from line 30. It basically tells the game: If the number of players is less than two, break the loop (i.e skip the intermission.)

Also, instead of using a value for NumPlayers, why not just use #game.Players:GetPlayers() everytime you want to use it. It updates automatically by getting how many people are in the game.

0
I have tried to put #game.Players:GetPlayer() but for some reason it glitches out a lot more than the Value does, just didn't want to take any chances of making the gameplay worse cmgtotalyawesome 1418 — 6y

Answer this question