Assigned Teammate Script not working properly

Hello again! It seems I find myself on this forum more often than I thought I would be…

Anyways, today my main project is just making a teammate system, which the server assigns you one when you join (if someone else is also awaiting a teammate. If everyone has a teammate, it puts you on a queue and waits for a new player to join.) I can’t seem to figure this out, though. I scripted everything properly, no errors in the output, but for some reason, it keeps printing that everyone has a teammate, even though they don’t.

Script in ServerScriptService:

local playerList = {}

game.Players.PlayerAdded:Connect(function(player)
	print(player.Name.." joined.")
	table.insert(playerList, #game.Players:GetPlayers(), player)
	print(player.Name.." added to the playerList table.")
	wait(1)
	if player:WaitForChild("Teams").AssignedTeammate.Value == nil then
		print(player.Name.." has no teammate.")
		game.ReplicatedStorage.Teams.PlayerNeedsTeammate:FireClient(player)
		print(player.Name.." has a new event.")
		game.ReplicatedStorage.Teams.PlayerNeedsTeammate.OnServerEvent:Connect(function(playerWhoFired)
			if playerWhoFired.Name == player.Name then
				print(player.Name.." reflected the event.")
				for _, plr in pairs(playerList) do
					if (plr.Name ~= player.Name) and plr:WaitForChild("Teams"):WaitForChild("AssignedTeammate").Value == nil then
						print(plr.Name.." is ready for a new teammate.")
						plr.Teams.AssignedTeammate.Value = player
						print(plr.Name.." got a new assigned teammate.")
					elseif plr.Name == player.Name then
						print("same player")
					elseif not plr.Teams.AssignedTeammate.Value == nil then
						print(plr.Name.." already has a teammate.")
					else
						print("an error occured.")
					end
				end
			end
		end)
	else
		print(player.Name.." already has a teammate.")
	end
end)

I also have a LocalScript in a GUI who tells you which player is your teammate:

local player = game.Players.LocalPlayer

game.ReplicatedStorage.Teams.PlayerNeedsTeammate.OnClientEvent:Connect(function()
	game.ReplicatedStorage.Teams.PlayerNeedsTeammate:FireServer(player)
end)

Again, though it just keeps printing out that everyone has a teammate (I’m using Studio Local Server), but when I check the explorer, everyone has a nil AssignedTeammate value.

Any help is appreciated, and if there’s any confusion, I’d be happy to help. :slight_smile: Thanks!

Because there are two prints that describe the player already having a teammate, could you replace that with error, so as to create a traceback and a line number in the output to work from? You might as well do this for all unwanted outcomes actually.
e.g.

-- at the bottom of your upper code block,

	else
		error(player.Name.." already has a teammate")
	end
end)

It also helps to print everything affecting the outcome as well, like printing plr.Teams.AssignedTeammate.Value and plr.Name / player.Name every iteration of the for loop, and the same for the player passed in from game.Players.PlayerAdded, their assigned teammate specifically.

OK, I’ll test this out. Thanks!

1 Like

This was the issue, line 31 of my code (near end). I don’t know how to fix this, though. It’s reading that the value isn’t nil, when it is.

2 Likes