Random team selection sometime not working

So,

Im making a horror game about a team (Home Owners) inside a house trying to protect the house and survive from the other team (Intruder).

So when you spawn, you get randomly inside a team. The intruder team has a maximum of 1 player, the rest of the players will spawn in the other team.

The problem is that sometimes:
-you don’t spawn in a team
-no one spawn in the intruder team

Here is the random team selection script: (Placed in ServerScriptService)

game.Players.PlayerAdded:Connect(function(Player)
	local Teams = game:GetService("Teams"):GetTeams()
	local RandomTeam = Teams[math.random(1,#Teams)]

	if RandomTeam.Name == "Intruders" then
		
		if #RandomTeam:GetPlayers() < 1 then
			Player.Team = RandomTeam --Intruder
			
			local PointLight = Instance.new("PointLight")
			
			PointLight.Enabled = true
			PointLight.Color = Color3.fromRGB(80, 0, 0)
			PointLight.Brightness = 0.6
			PointLight.Shadows = true
			PointLight.Range = 6
			
			wait(1)
			
			PointLight.Parent = Player.Character.Torso
		end
	else
		Player.Team = RandomTeam --Home Owner
		
		local PointLight = Instance.new("PointLight")

		PointLight.Enabled = true
		PointLight.Color = Color3.fromRGB(255, 255, 255)
		PointLight.Brightness = 0.2
		PointLight.Shadows = true
		PointLight.Range = 4
		
		wait(1)
		
		PointLight.Parent = Player.Character.Torso
	end
end)

The point light is for a different purpose.

If the intruders team is full, you don’t have it setting the team. You also have no way to insure that the intruders team has someone in it. Here is a script that includes both of those, and also a way to detect if the intruder leaves the game:

local intruder = nil

game.Players.PlayerAdded:Connect(function(Player)
	local Teams = game:GetService("Teams"):GetTeams()
	local RandomTeam = Teams[math.random(1,#Teams)]
	
	if RandomTeam.Name == "Intruders" then
		if #RandomTeam:GetPlayers() < 1 then
			Player.Team = RandomTeam --Intruder
			intruder = Player -- sets intruder

			local PointLight = Instance.new("PointLight")

			PointLight.Enabled = true
			PointLight.Color = Color3.fromRGB(80, 0, 0)
			PointLight.Brightness = 0.6
			PointLight.Shadows = true
			PointLight.Range = 6

			wait(1)

			PointLight.Parent = Player.Character.Torso
		else
			Player.Team = game:GetService("Teams").Homeowners -- you may need to edit to be the correct name
		end
	else
		Player.Team = RandomTeam --Home Owner

		local PointLight = Instance.new("PointLight")

		PointLight.Enabled = true
		PointLight.Color = Color3.fromRGB(255, 255, 255)
		PointLight.Brightness = 0.2
		PointLight.Shadows = true
		PointLight.Range = 4

		wait(1)

		PointLight.Parent = Player.Character.Torso
	end
	if not intruder then
		Player.Team = game:GetService("Teams").Intruders
		intruder = Player
	end
end)

-- makes it so if the intruder leaves, the intruder is set back to nil. Note that this will not set a new intruder
game.Players.PlayerRemoving:Connect(function(Player)
	if Player == intruder then
		intruder = nil
	end
end)

Though with how you have it set up, the first person who joins will always be the intruder. The best way to fix this is instead of when a player is added it does this, it does it at the start of the round. If you need help doing that let me know.

1 Like

Thank you very much !

I didn’t thinked about the intruder leaving the game, thank you for that too !

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.