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. Thanks!