Assign team wont work

my team assigning wont work? and i dont really know how to fix it, the the other parts of the script work but just not assign team

local Workspace = game.Workspace
local Players = game:GetService("Players")
local Teams = game:GetService("Teams")
local waiting = Teams:WaitForChild("Waiting")

local Part1 = Workspace["part 1"]
local Part2 = Workspace["part 2"]
local CountdownEvent = game.ReplicatedStorage.CountdownEvent

local gameState = "Intermission" -- keep track of the games state

local killerTeam = Teams:WaitForChild("killer")
local survivorTeam = Teams:WaitForChild("survivor")

local function assignTeams()
	local killerExists = false

	for _, player in Players:GetPlayers() do
		if player.Team == killerTeam then
			killerExists = true
			break
		end
	end

	if not killerExists then
		local players = Players:GetPlayers()
		if #players > 0 then
			local randomPlayer = players[math.random(1, #players)]
			randomPlayer.Team = killerTeam
		end
	end

	for _, player in Players:GetPlayers() do
		if player.Team ~= killerTeam then
			player.Team = survivorTeam
		end
	end
end

local function teleportPlayer(player, part)
	if player.Character then
		player.Character:MoveTo(part.Position)
	end
end

local function teleportAllPlayers(part)
	for _, player in Players:GetPlayers() do
		teleportPlayer(player, part)
	end
end

local function onPlayerAdded(player)
	player.Team = waiting -- Assign the player to the waiting team upon joining
	player.CharacterAdded:Connect(function()
		if gameState == "Time Left" then
			teleportPlayer(player, Part2)
		end
	end)
end

local function countdown(time, message)
	gameState = message -- update the games state
	for i = time, 0, -1 do
		CountdownEvent:FireAllClients(message .. ": " .. i)
		wait(1)
	end
end

-- listen to the playeradded event
Players.PlayerAdded:Connect(onPlayerAdded)

while true do
	countdown(4, "Intermission")
	teleportAllPlayers(Part2)
	for _, player in Players:GetPlayers() do
		player.Team = waiting -- Assign all players to the waiting team during intermission
	end

	assignTeams() -- Ensure teams are assigned at the start of the game

	countdown(3, "Time Left")
	teleportAllPlayers(Part1)
	for _, player in Players:GetPlayers() do
		player.OnTeleport:Connect(function()
			player.Team = waiting
		end)
	end
	CountdownEvent:FireAllClients("Round Over")
	wait(2)
	gameState = "Intermission" -- reset the game state
end


Please Help!

Seems like you have multiple codes within the while loop whose purpose is just to assign teams. Could be that they’re conflicting with one another. Don’t think you need to run it that much. Just some optimization suggestion. Plus, you have loops after loops. For loops within the while loop, and your countdown function also has a loop in it and a wait. I reckon there are more optimal ways to do this.

i understood nothing :sob: :sob: :sob: thanks tho ill try understanding it

local function assignTeams()
	local players = Players:GetPlayers()
	
	if #players == 0 then return end

	local randomPlayer = players[math.random(1, #players)]
	randomPlayer.Team = killerTeam

	for _, player in players do
		if player ~= randomPlayer then
			player.Team = survivorTeam
		end
	end
end

Try this have no time to explain rgt now

1 Like