i am trying to assign players to random but even teams, but the code also considers friends so if you’re friends with someone you are more likely to be in the same team, but the priority is to evenness
the thing is when i am assigned for say “blue team” i am forced back for some reason to “red team”? auto assignable is turn off so thats not the issue, here is the output i did a of printing and warning to see whats wrong
17:22:24.447 Player added: lollipop_anim - Server - Server:103
17:22:24.447 Assigning team for lollipop_anim - Server - Server:28
17:22:24.447 Team sizes: Blue = 0 Red = 0 - Server - Server:23
17:22:24.448 Friend teams for lollipop_anim : {} - Server - Server:61
17:22:24.448 Chose random team Blue for lollipop_anim - Server - Server:93
17:22:24.448 lollipop_anim assigned to Blue (Blue: 1 Red: 0 ) - Server - Server:99
17:22:24.545 Failed to load plugin type=Cloud assetId=4725618216 assetVersionId=12581033915 in datamodel StudioGameStateType_PlayServer - Studio
17:22:24.548 Failed to load plugin type=Cloud assetId=1496745047 assetVersionId=10356989552 in datamodel StudioGameStateType_PlayServer - Studio
17:22:24.660 Failed to load plugin type=Cloud assetId=4725618216 assetVersionId=12581033915 in datamodel StudioGameStateType_PlayClient - Studio
17:22:24.663 Failed to load plugin type=Cloud assetId=1496745047 assetVersionId=10356989552 in datamodel StudioGameStateType_PlayClient - Studio
17:22:25.360 Current team assignments: - Server - Server:122
17:22:25.360 Blue team players: 0 None - Server - Server:145
17:22:25.360 Red team players: 1 lollipop_anim - Server - Server:146
17:22:25.360 Blue Team: Blue Red Team: Red - Server - Server:148
17:22:25.360 GUI Objects Found - RedTeamElection: RedTeamElection BlueTeamElection: BlueTeamElection - Server - Server:156
17:22:25.361 lollipop_anim Team: Red (intended: Blue ) - Server - Server:160
17:22:25.361 Player lollipop_anim on wrong team Red intended Blue reassigning - Server - Server:165
17:22:25.361 lollipop_anim reassigned to Red - Server - Server:167
17:22:25.361 Cloned RedTeamElection for lollipop_anim - Server - Server:180
17:22:25.361 Sending teammates to lollipop_anim : ▶ {...} - Server - Server:188
i tried everything, the ai assistant thingie and even grok!!! but to no avail
here is the code
local Players = game:GetService("Players")
local Teams = game:GetService("Teams")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local handler = require(script:WaitForChild("ModuleHandler"))
-- Store intended team assignments to detect discrepancies
local intendedTeams = {}
local function getTeamSizes()
local blue = Teams:FindFirstChild("Blue")
local red = Teams:FindFirstChild("Red")
local blueSize = blue and #blue:GetPlayers() or 0
local redSize = red and #red:GetPlayers() or 0
print("Team sizes: Blue =", blueSize, "Red =", redSize)
return blueSize, redSize, blue, red
end
local function assignPlayerToTeam(player)
print("Assigning team for", player.Name)
if player.Team then
print(player.Name, "already has a team:", player.Team.Name)
return
end
local blueSize, redSize, blueTeam, redTeam = getTeamSizes()
if not blueTeam or not redTeam then
warn("No Blue or Red team found for", player.Name)
return
end
-- Find friends in the game
local friendTeams = {}
for _, otherPlayer in pairs(Players:GetPlayers()) do
if otherPlayer ~= player and otherPlayer.Team then
local isFriend = false
local success, result = pcall(function()
return player:IsFriendsWith(otherPlayer.UserId)
end)
if success then
if result then
isFriend = true
print(player.Name, "is friends with", otherPlayer.Name, "on team", otherPlayer.Team.Name)
end
else
warn("Failed to check friendship for", player.Name, "and", otherPlayer.Name)
end
if isFriend then
friendTeams[otherPlayer.Team] = (friendTeams[otherPlayer.Team] or 0) + 1
end
end
end
print("Friend teams for", player.Name, ":", friendTeams)
local chosenTeam = nil
if next(friendTeams) then
local viableFriendTeams = {}
for team, _ in pairs(friendTeams) do
local teamSize = (team == blueTeam) and blueSize or redSize
local otherTeamSize = (team == blueTeam) and redSize or blueSize
local sizeDiff = math.abs((teamSize + 1) - otherTeamSize)
print("Checking team", team.Name, ": Size =", teamSize, "Other size =", otherTeamSize, "Diff =", sizeDiff)
if sizeDiff <= 1 then
table.insert(viableFriendTeams, team)
end
end
if #viableFriendTeams > 0 then
chosenTeam = viableFriendTeams[math.random(1, #viableFriendTeams)]
print("Chose friend team", chosenTeam.Name, "for", player.Name)
end
end
if not chosenTeam then
if blueSize < redSize then
chosenTeam = blueTeam
print("Chose Blue (fewer players) for", player.Name)
elseif redSize < blueSize then
chosenTeam = redTeam
print("Chose Red (fewer players) for", player.Name)
else
chosenTeam = ({blueTeam, redTeam})[math.random(1, 2)]
print("Chose random team", chosenTeam.Name, "for", player.Name)
end
end
player.Team = chosenTeam
intendedTeams[player.UserId] = chosenTeam
print(player.Name, "assigned to", chosenTeam.Name, "(Blue:", blueSize + (chosenTeam == blueTeam and 1 or 0), "Red:", redSize + (chosenTeam == redTeam and 1 or 0), ")")
end
Players.PlayerAdded:Connect(function(player)
print("Player added:", player.Name)
assignPlayerToTeam(player)
player:GetPropertyChangedSignal("Team"):Connect(function()
local newTeam = player.Team and player.Team.Name or "No Team"
local intendedTeam = intendedTeams[player.UserId] and intendedTeams[player.UserId].Name or "None"
print("Team changed for", player.Name, "to", newTeam, "(intended:", intendedTeam, ")")
if newTeam == "No Team" then
warn("Unexpected team clear for", player.Name, "reassigning")
assignPlayerToTeam(player)
elseif newTeam ~= intendedTeam then
warn("Unexpected team change for", player.Name, "from intended", intendedTeam, "to", newTeam, "reassigning")
player.Team = intendedTeams[player.UserId] or Teams:FindFirstChild("Blue") or Teams:FindFirstChild("Red")
print(player.Name, "reassigned to", player.Team.Name)
end
startElection()
end)
end)
thank you for reading