Teams being chosen before round starts

I made 2 scripts. One script sets an intermission time in the lobby, and when it’s done it sets a timer for the game map and teleports the players to the game area. The other script organizes the teams by selecting one random user to be on Death while everyone else are Runners.

Team Organizing Script:

local plrs = game.Players
local runners = {}

local death = game.Teams.Death
local runners = game.Teams.Runners

inRound = game.ReplicatedStorage.InRound

while wait(10) do
    local chosen = plrs:GetChildren()[math.random(1, #plrs:GetChildren())]

    for i, plr in pairs(plrs:GetChildren()) do 
	    if plr ~= chosen then
		    table.insert(runners, plr)
		    plr.Team = runners
	    else
		    plr.Team = death
	    end
    end
end

Round/Intermission Timer Script:

 local music = game.Workspace.music
local music1 = game.Workspace.music_duplex

local title = game.StarterGui.Menu.TitleScreen

InRound.Changed:Connect(function()
    if InRound.Value == true then	
	    for _, player in pairs(game.Players:GetChildren()) do
	    	    local char = player.Character
		    char.HumanoidRootPart.CFrame = gamespawn.CFrame
	    end
    else
	    for _, player in pairs(game.Players:GetChildren()) do
		    local char = player.Character
		    char.HumanoidRootPart.CFrame = lobbyspawn.CFrame
	    end		
    end
end)

local function RoundTimer()
    while wait() do
	    music:Play()
	    music1:Stop()
	    for i = intermissionLength, 0, -1 do
		    InRound.Value = false
		    wait(1)
		    status.Value = "Intermission: ".. i .." seconds left!"
	    end
	    music:Stop()
	    music1:Play()
	    for i = roundLength, 0, -1 do
		    InRound.Value = true
		    wait(1)
		    status.Value = "Game: ".. i .." seconds left!"
	    end
    end
end

spawn(RoundTimer)

Individually they work fine, but I needed to organize the teams when the round started instead of all the time. So, in the team script, instead of “while wait(10) do” I put “while InRound do” so it would only do teams when the round was in session. It didn’t work. I tried similar variations of the code, but I couldn’t figure it out. Anyone have any idea on how I could figure this out? If I organize the teams when the round starts I could teleport Death to a different starting location than the other team.

Any help would be greatly appreciated!

2 Likes

Hey there!

I don’t think you are using the most effective approach for this issue.
The Team Organizing merely depends on the inRound boolValue. If this is all for a game round loop, why don’t you start with the round loop script itself. This way, you can run the Team Organizing once and you can be confident it will not reorganize teams until next round.

I’m going to be straight with you, I’m not sure what that means. Could you elaborate?

If I understand correctly, your game loop restarts every time your boolValue “InRound” goes from true to false and vice-versa. This brings up some issues as the one you are currently trying to fix. What most round based games do is have a while loop run. This ensures your team organizing script will run as desired, and it will also let you call your round and intermission timer scripts at the right times.

If by while loop you mean putting the line of code as “while inRound.Changed do” or anything related to that it did not work. Please forgive my misunderstandings, I’m still really new at scripting.

It’s fine. This is a very basic example of a while loop used to run a game:

while true do
	repeat wait() until #game.Players:GetPlayers() >= 2 --Usually you need a server to have at least 2 players
	functions.Intermission()
	functions.SetPlayers()
	functions.Begin()
	functions.Cleanup()
end

Create a ModuleScript with functions for each one of the parts of a round!

I don’t know what a module script is. I looked it up but I don’t know to apply it to what you’re saying.

But once I know what it is, do I just do it and place another script in ServerScriptService with your code in it? Never thought a team script would be this complex, I have a lot to learn about lua.