Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Randomly choose a team? [Help] [If it works, I'll give credit]

Asked by 10 years ago

So, I'm developing a game called 'Bullseye', I'm working on adding a lobby script and I've done most of it but I don't know how to implement teams into the script, if someone could help me I would really appreciate it, the teams are "Feds", "Terrorists" & "Civilians", I need to somehow make the script randomly place players into one of the teams. [Feds should have 2nd most players, Civilians should have the most players and Terrorists should have around 2-3 players in it]

Thanks for any help! Your help is greatly appreciated!

Here is the script:

while true do
local start = 20

local hint = Instance.new("Hint") 
hint.Parent = game.Workspace 
for i = 1,start do
hint.Text = start - i 
wait(1) 
end
hint.Text = "Game Starting" -- Change this to what you want it to say when its starting the game
wait(2)
hint:remove()
-- target\/                              or                  or
random = CFrame.new(-122.6, 4.59, -199.6 or -104.6, 4.59, -85 or -132.6, 4.59, 13.6) -- Location you want them to go
for i, player in pairs(game.Players:GetChildren()) do
player.Character.Torso.CFrame = random + Vector3.new(0, i * 5, 0)
end
-- random after cframe = target
local start = 500
for i = 1,start do
hint.Text = start - i 
wait(1) 
end
hint.Text = "Game Over" -- Lose message
p = game.Players:GetChildren()
for i = 1,#p do
p[i].Character.Head:remove()
end
wait(2)
hint:remove()

end
0
Put the code in code block so we can tell what it says Tempestatem 884 — 10y
0
Ok, done. Kinng1826 0 — 10y

1 answer

Log in to vote
0
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
10 years ago

This question can pretty much be answered its own vacuum without the clutter of the rest of whatever else you're doing (elegant code is modular after all).

So, your question is basically:

How can I break a list of things randomly into 3 groups of different sizes?

This question has two parts. 1). What are those sizes? 2). How do we pick randomly for those sizes?



Picking Three Sizes

We can call those sizes A, B, and C where A > B > C. If we have P players in our game, then we know that A + B + C = P.

You also said that 4 > C.

There's a lot of different ways that you could assign this. Here's just one way that may be what you're looking for, or it might need some tweaking.

Let's say that the medium team (B) has 1 + C + floor(14% * P), and that A is 50% of the players.

Then we get something like this:

local A = math.ceil(P / 2);
local R = P - A; -- R for Remainder / Remaining

We want then B + C = R and we say B = 1 + C + floor(14% * P) ~~ 1 + C + floor(28% * R). This is equivalent then to floor(28% * R) + C + C + 1 = floor(28% * R) + 2 * C + 1 = R, so now we have to solve for C.

C = (R - 1 - floor( 0.28 * R) ) / 2

And B = R - C. So altogether:

local A = math.ceil(P / 2);
local R = P - A; -- R for Remainder / Remaining
local C = math.floor( (R - 1 - math.floor( 0.28 * R) )  / 2);
local B = R - C;

Here's what these team sizes look like:

P   |   A   B   C
--------------
6   |   3   2   1
9   |   5   3   1
12  |   6   4   2
15  |   8   5   2
18  |   9   6   3
21  |   11  7   3
24  |   12  8   4

(Where P again is the number of players playing, and A, B, and C are the sizes of the teams from largest to smallest). The numbers may need tweaking. Alternatively, if you don't want to use math to do it, you can just make your own table for each of the number of players that your game allows:

local As = {0,0,0,0,0,0,3,4,4,5,5,6,6,...};
local Bs = {0,0,0,0,0,0,2,2,3,3,4,4,4,...};
local Cs = {0,0,0,0,0,0,1,1,1,1,1,2,2,...};

local A,B,C = As[P], Bs[P], Cs[P];


Randomizing Teams

So once you have calculated the sizes you want for each team, you just have to pick that many players to go on that team.

There's a few ways to do this. The shortest way would probably be to get a list of players, shuffle it, and then assign the first A to team A, the next B to team B, and the rest to team C. That could look like this:

local players = game.Players:GetPlayers();
local n = #t
for n = #t,2,-1 do
    -- Fisher-Yates fair shuffle algorithm
    -- Adapted from RosettaCode
    local k = math.random(n);
    players[n], players[k] = players[k], players[n];
end

for n = 1,#players do
    players[n].TeamColor = BrickColor.new("Bright green");
    -- This is your C, smallest, team
end

for n = 1,A do
    players[n].TeamColor = BrickColor.new("Bright red");
    -- Or whatever team color
    -- This is your A, biggest team
end
for n = A+1,A+B do
    players[n].TeamColor = BrickColor.new("Bright blue"); -- Or w/e
    -- This is your B, medium sized team
end

Hopefully this is more than enough!

Ad

Answer this question