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