Teams are not randomly being selected for each player

I have been having some trouble recently, trying to make a system where, at the click of a gui button, it gets every player currently in the game, and assigns them each, a random role.
Currently, when this is run, the players all go onto the same team name for some reason, and on the server it shows that the players have a different team, compared to looking at their teams on the client. none of the teams have the “auto assignable” property set to true.

local rEvent = game.ReplicatedStorage.rEvents.rolesAssignment
local players = game:GetService("Players")
local teams = game:GetService("Teams")

rEvent.OnServerEvent:Connect(function()
    for i,player in pairs(game.Players:GetPlayers()) do
        if player then
            if player.Team == nil then
                local roles = teams:GetChildren()
                local packLeader = teams.PackLeader
                local randomTeam = roles[math.random(1, #roles)]
                if #packLeader:GetPlayers() == 1 and randomTeam.Name == "PackLeader" then repeat
                        randomTeam = roles[math.random(1, #roles)]
                    until
                    randomTeam.Name ~= "PackLeader"
                end
                player.Team = randomTeam
                print(player.Name.."is now in the team:   "..player.Team.Name)
                task.wait(2)
            end
        end
    end
end)

i will link screenshots below as of what the script does currently. keep in mind, running these scripts were done on a local 4 player server, 3 players in the server returned the “tank” role, and 1 returned the “knight” role, none of which became villagers.

ive looked everywhere on developer forum, youtube and some community servers and nothing has worked.
image
image
image
image

1 Like

if i wasnt clear enough on the post, in 3 of the 4 different client views, every player had the “tank role”, and in one of the four client views, every player had the “knight” role

1 Like

ive been trying to find a fix STILL since this post was made, and havent found one yet. ive tried making a table of the team names and finding them in the teams folder and it still hasnt worked. please, if you know how to help it would be super appreciative

1 Like

Apparently, the same role was being selected because all of the teams had the same team color, and because of that, they deleted eachother until 1 was remaining, apparently the 1 remaining is always villager so yeah. Hope this post helps anyone in the future.

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.