So this is an addition to my last script with the mapVoting / timer. I added a splittingPlayers function that is supposed to sort players into different teams, though when I had it completely done, an error occurred that said: "exhausted allowed execution time." I assumed this meant that the script itself was too long, so I moved the splittingPlayers function to a module script and tried to call it, but now I am receiving the error: "attempt to call a nil value." I am not surprised though since I probably made this function in the most inefficient way possible, but this was the only way I knew how to do it.

Here are some details:

-the "Spotters" team is supposed to have fewer players than the fugitives, and depending on how many players are in the server (playernum >= 12, playernum < 12 and playernum >= 8, etc.) a certain amount of players are randomly picked for this team every round.

-The Module script and the script that uses the splittingPlayers function are both in ServerScriptService.

-The script that uses the function is a normal script.

-There are no script analysis errors

If you know a better way to go about this or if you can find an error with my function, please let me know

Thank you.

local playernum = game:WaitForChild("Players").NumPlayers
local splitPlayers = {}



function splitPlayers.splttingPlayers()
	--Splitting the teams
	local AllPlayers = game:WaitForChild("Players"):GetPlayers()
	if playernum >= 12 then
		local spotter1 = AllPlayers[math.random(1, #AllPlayers)]
		local spotter2 = AllPlayers[math.random(1, #AllPlayers)]
		local spotter3 = AllPlayers[math.random(1, #AllPlayers)]
		local spotter4 = AllPlayers[math.random(1, #AllPlayers)]
		if spotter2 == spotter1 then
			repeat
				spotter2 = AllPlayers[math.random(1, #AllPlayers)]
			until spotter2 ~= spotter1
		end
		if spotter3 == spotter1 or spotter3 == spotter2 then
			repeat
				spotter3 = AllPlayers[math.random(1, #AllPlayers)]
			until spotter3 ~= spotter1 and spotter3 ~= spotter2
		end
		if spotter4 == spotter1 or spotter4 == spotter2 or spotter4 == spotter3 then
			repeat
				spotter4 = AllPlayers[math.random(1, #AllPlayers)]
			until spotter4 ~= spotter1 and spotter4 ~= spotter2 and spotter4 ~= spotter3
		end
		spotter1.Team = game:WaitForChild("Teams").Spotters
		spotter2.Team = game:WaitForChild("Teams").Spotters
		spotter3.Team = game:WaitForChild("Teams").Spotters
		spotter4.Team = game:WaitForChild("Teams").Spotters
	elseif playernum < 12 and playernum >= 8 then
		local spotter1 = AllPlayers[math.random(1, #AllPlayers)]
		local spotter2 = AllPlayers[math.random(1, #AllPlayers)]
		local spotter3 = AllPlayers[math.random(1, #AllPlayers)]
		if spotter2 == spotter1 then
			repeat
				spotter2 = AllPlayers[math.random(1, #AllPlayers)]
			until spotter2 ~= spotter1
		end
		if spotter3 == spotter1 or spotter3 == spotter2 then
			repeat
				spotter3 = AllPlayers[math.random(1, #AllPlayers)]
			until spotter3 ~= spotter1 and spotter3 ~= spotter2
		end
		spotter1.Team = game:WaitForChild("Teams").Spotters
		spotter2.Team = game:WaitForChild("Teams").Spotters
		spotter3.Team = game:WaitForChild("Teams").Spotters
	elseif playernum < 8 and playernum >= 4 then
		local spotter1 = AllPlayers[math.random(1, #AllPlayers)]
		local spotter2 = AllPlayers[math.random(1, #AllPlayers)]
		if spotter2 == spotter1 then
			repeat
				spotter2 = AllPlayers[math.random(1, #AllPlayers)]
			until spotter2 ~= spotter1
		end
		spotter1.Team = game:WaitForChild("Teams").Spotters
		spotter2.Team = game:WaitForChild("Teams").Spotters
	elseif playernum < 4 then
		local spotter1 = AllPlayers[math.random(1, #AllPlayers)]
		spotter1.Team = game:WaitForChild("Teams").Spotters
	end
	wait(0.1)
	local lobbyNum = game:WaitForChild("Teams"):WaitForChild("Lobby"):GetPlayers()
	repeat
		local player = AllPlayers[math.random(1, #AllPlayers)]
		if player.Team == game:WaitForChild("Teams").Lobby then
			player.Team = game:WaitForChild("Teams").Fugitives
		end
	until lobbyNum == 0
end

return splitPlayers