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

Is there a problem with this function?

Asked by
Irvene 5
10 years ago

When the game starts, it's supposed to team players on red, and blue and it doesn't do so. Can someone help me out? Is the function not proper?

function teams()
local balanceTeams = function(players, teams, randomize, callback)
for key = 1, #players do
local value = table.remove(players, randomize and math.random(#players) or 1);
if (not callback) or callback(key, value) then
value.TeamColor = teams[(key % (#teams + 1)) + 1];
end
end
end;

balanceTeams(
players:GetPlayers(), -- List of all players
{BrickColor.new("Bright red"), BrickColor.new("Bright blue")}, -- List of all teams
true -- Should it be random or not?
);

0
Try adding game.Players.PlayerAdded:connect(teams) to the bottom of your script. TheStudentPilot 75 — 10y

1 answer

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

You should tab & space your code properly

function teams()
    local balanceTeams = function(players, teams, randomize, callback)
        for key = 1, #players do
            local value = table.remove(players, randomize and math.random(#players) or 1);
            if (not callback) or callback(key, value) then
                value.TeamColor = teams[(key % (#teams + 1)) + 1];
            end
        end
    end;

    balanceTeams(
    players:GetPlayers(), -- List of all players
    {BrickColor.new("Bright red"), BrickColor.new("Bright blue")}, -- List of all teams
    true -- Should it be random or not?
    );

You will see you are missing a final end and that teams() is never called.


There is only one more trouble with this script:

teams[(key % (#teams + 1)) + 1];

#teams + 1 should be replaced with just #teams.

When you do mod, you exclude the top value (#teams) -- however, you do include 0. So mod #teams + 1 actually allows #teams + 1 values.

(Just as the correct % #teams allows for #teams different values, [0, 1, 2, .., #teams - 1])

Ad

Answer this question