Random teams code needs improving

code.lua (727 Bytes)

  • What does the code do and what are you not satisfied with?

The code I wrote assigns players a team based on a certain percentage. It works as intended, however, the code assigns people the same team every time it runs unless a new player is added or a player left. Simplified; if there are 3 people, one player will be assigned the same team over and over again every time the code is run, there is no variance.

The percentages are also slightly incorrect (I believe?) but I don’t want to change them due to the risk of breaking the script or making it unbalanced.

  • What potential improvements have you considered?

I rewrote the percentages a few times but still no difference.

  • How (specifically) do you want to improve the code?

I want it to be completely random, based on the percentages provided, no matter if the same people are in the game or not.

function AssignTeams()
			
	local plrs = game.Players:GetPlayers()

	for i, plr in pairs(plrs) do
		
		local p = i/#plrs
		
		if p < 0.25 then -- i would like this team to be the rarest
				
			plr.Team = game.Teams.Red
			plr:LoadCharacter()

		
		elseif p < 0.50 then -- i would like this team percentage to be the same as yellow but with a 5% less chance
			
			plr.Team = game.Teams.Grey
			plr:LoadCharacter()
		
			
		elseif p < 0.75 then -- i would like this team percentage to be the same as grey but with an increased 5% chance
			
			plr.Team = game.Teams.Yellow
			plr:LoadCharacter()

			
		else -- everyone else gets assigned this team
			
			plr.Team = game.Teams.Orange
			plr:LoadCharacter()
	
		end
	end	
end

I’m not sure how you assign p, but you should be using math.random() and use it inside your AssignTeams() function so that it always is different.

You also have your if statements for p setup poorly, as it could be .1 and every elseif would run since its lower than .25. You could add a > also to prevent it from doing that.

Also, you could just use a PlayerAdded function instead of your loop of all players finding the one you want. This would all look like

game.Players.PlayerAdded:Connect(function(plr)
    local p = math.random(1,100) / 100
    if p < .25 then
        --assign team
    elseif p > .25 and p < .50 then
        --assign team
    elseif p > .50 and p < .75 then
        --assign team
    elseif p > .75 then
        --assign team
    end
end)
1 Like

I only want the function to run when called, but I will take your other advice. Thanks.

Use Random to assign teams. Also, your if statements are poorly organized and set up and use PlayerAdded to assign teams so for every player join, they get assigned to a team. Also use local functions over global ones since they are faster. Also, instead of calling PlayerAdded Event on the function, call the function on player added instead and passing the argument for player.

Here’s how I would write your code:

local function AssignTeams(player)

        local randomTeams = {

          [.25] = game.Teams.Red,
          [.50] = game.Teams.Grey,
          [.75] = game.Teams.Orange

        }

        local Random = Random.new()
        local randomChance = Random:NextInteger(1, 100) / 100 -- Get's a random number from 100 and divides it by 100

        player.Team = randomTeams[randomChance]
end

game.Players.PlayerAdded:Connect(AssignTeams)
1 Like

What if I want to assign all players to the teams I provided and call it without using PlayerAdded and instead a function?

If I understand your reply correctly, looping through players should work.

But that will only get the current players, it won’t affect players who joined after which is what he doesn’t want unless he calls the function frequently which isn’t good at the same time.

PlayerAdded is the way to go since he can just call the function whenever a new player joins.

Take that example from my script.

1 Like

I only call the function once every “round”. It just assigns players to teams and starts said "round’. Having it fire everytime a player joins doesn’t do what its supposed to. All players start on the Lobby team then get assigned a team when the game starts.

Then a for loop would work.

Example:

for _,player in pairs (game.Players:GetPlayers())
--Assign a team to player
end

Running that everytime a round starts should work.

I already used a for loop. That dosen’t help with percentages and randomising.

Try this:

for _,player in pairs (game.Players:GetPlayers())

local number = math.random(1,100)

if number > 0 and number <= 50 then
--Assign common team

elseif number > 50 and number <= 95 then
--Assign common but 5% less team

elseif number > 95 then
--Assign rare team
end
end

The code might not be perfect I’m a little tired sorry if it isn’t.

Play around with the numbers if you wanna change the chances btw

3 Likes

I’ll give it a test and let you know how it went

1 Like

It works lovely! Thanks for that.

1 Like