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)
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)
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.
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.
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