Attempting to index nil with GetPlayers()

Hi, I was improving the scripts for my game and i made this module script activated by a Script OnServerEvent:

local MPS = game:GetService("MarketplaceService")

local Teams = game:GetService("Teams")

local developers = {
	1631420942;
	1280667039; 
	1363402413; 
	1115378794, 
}

local functions = {}

function functions.changeTeam(player, team, kill, maxPlayers, container)
	if team ~= "SWAT" then
		if player.Team ~= game.Teams:FindFirstChild(team) then
			if maxPlayers == false then
				player.Team = game.Teams:FindFirstChild(team)
				if kill == true then
					player.Character.Humanoid.Health = 0
				end
			else
				local playersInTeam = game.Teams:FindFirstChild(team):GetPlayers()
				
				if playersInTeam then
					if #playersInTeam < maxPlayers then
						player.Team = game.Teams:FindFirstChild(team)
						if kill == true then
							player.Character.Humanoid.Health = 0
						end
					else
						if not table.find(developers, player.UserId) then
							functions.write(container, "This team is full.")
						else
							if player.Team ~= game.Teams:FindFirstChild(team) then
								player.Team = game.Teams:FindFirstChild(team)
								if kill == true then
									player.Character.Humanoid.Health = 0
								end
							end
						end
					end
				end
			end
		end
	else
		if MPS:UserOwnsGamePassAsync(player.UserId, 17525592) or table.find(developers, player.UserId) then
			if maxPlayers == false then
				if player.Team ~= game.Teams:FindFirstChild(team) then
					player.Team = game.Teams:FindFirstChild(team)
					if kill == true then
						player.Character.Humanoid.Health = 0
					end
				end
			else
				local playersInTeam = game.Teams:FindFirstChild(team):GetPlayers()
				
				if playersInTeam then
					if #playersInTeam < maxPlayers then
						if player.Team ~= game.Teams:FindFirstChild(team) then
							player.Team = game.Teams:FindFirstChild(team)
							if kill == true then
								player.Character.Humanoid.Health = 0
							end
						end
					else
						if not table.find(developers, player.UserId) then
							functions.write(container, "This team is full.")
						else
							if player.Team ~= game.Teams:FindFirstChild(team) then
								player.Team = game.Teams:FindFirstChild(team)
								if kill == true then
									player.Character.Humanoid.Health = 0
								end
							end
						end
					end
				end
			end
		else
			MPS:PromptGamePassPurchase(player, 17525592)
		end
	end
end

return functions

It works but when the player changes team it starts spamming the output with this error:

ServerScriptService.Functions:34: attempt to index nil with 'GetPlayers

It is like if it would be looped, do you know why it does this? Help is appreciated!

That means that it didn’t find the team using FindFirstChild. FindFirstChild is returning nil, thus calling GetPlayers can’t be done (that’s what the error is saying - you’re trying to index nil).

You should probably do some pre-checks to make sure that the team argument coming in is a valid team.

Ok i will try the thing i was thinking is why it doesn’t stop?