I've been trying to get this to work for the past few days and I can officially admit that I am at a loss. The main gist of this code is that it creates a table, puts all the players inside of it, then, depending on the size of the server, it randomly picks players out of the table to be on a specific team. Afterward, the players left in the table are sent to a separate team.

This is my problem, I am using a for key, value loop, and it appears that the value it randomly picks and the key it picks are both in different positions in the table.

I'm not gonna fake it, I don't know how a lot of this works, and I am trying to understand, but I don't have anyone that I can ask questions to if I am confused about a certain concept. I've been building since 2013, and I am not experienced in scripting; I am working alone on my game, and all I got are tutorials and dev forums. So if you know what is wrong, could you try to help me out/explain it to me so that I can learn.

Here is the code:

local function pickingSpotters()
	if playernum >= 10 then
		local count = 5
		for key, value in pairs (PlayersToTeams) do
			value = PlayersToTeams[math.random(1,#PlayersToTeams)]
			value.Team = game.Teams.Spotters
			table.remove(PlayersToTeams,key)
			count = count - 1
			if count == 0 then
				break
			end
		end
	elseif playernum <= 9 and playernum >= 8 then
		local count = 4
		for key, value in pairs (PlayersToTeams) do
			value = PlayersToTeams[math.random(1,#PlayersToTeams)]
			value.Team = game.Teams.Spotters
			table.remove(PlayersToTeams,key)
			count = count - 1
			if count == 0 then
				break
			end
		end
	elseif playernum <= 7 and playernum >= 6 then
		local count = 3
		for key, value in pairs (PlayersToTeams) do
			value = PlayersToTeams[math.random(1,#PlayersToTeams)]
			value.Team = game.Teams.Spotters
			table.remove(PlayersToTeams,key)
			count = count - 1
			if count == 0 then
				break
			end
		end

	elseif playernum <= 5 and playernum >= 4 then
		local count = 2
		for key, value in pairs (PlayersToTeams) do
			value = PlayersToTeams[math.random(1,#PlayersToTeams)]
			value.Team = game.Teams.Spotters
			table.remove(PlayersToTeams,key)
			count = count - 1
			if count == 0 then
				break
			end
		end
	else
		local count = 1
		for key, value in pairs (PlayersToTeams) do
			value = PlayersToTeams[math.random(1,#PlayersToTeams)]
			print (value, key)
			value.Team = game.Teams.Spotters
			table.remove(PlayersToTeams,key)
			count = count - 1
			if count == 0 then
				break
			end
		end
	end
end

So at first, I thought that my problem was that it just wasn't removing things from the table like it should. However, when I printed both the key and the value, I noticed that it printed out 2 different indexes. I confirmed this theory while running a Local Server with 3 clients when I noticed that the value that was randomly picked (Player3, for example) had changed teams, while the key that was selected had been removed, it was just another player (Player2, for example). This caused Player3 to be immediately sent to the other team after first being sent to the "Spotters" team as its key was not removed from the table, and Player2 to be sent to no team at all and remain in the default/lobby team.

What I want is for the value that is selected, and the key that is selected, to be under the same client so that only Player3 is removed from the table and he successfully changes his team.

3 comments

0

what is PlayersToTeams? Please include that variabl

0

Also, playernum cannot be 4.1 or 4.2 etc so you do not need to do "if playernum <= 5 and playernum >= 4", just do something like "if playernum == 5" or something

0

Okay. So PlayersToTeams is the table all the players are added to at the start