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

What's the error in the teaming function in this script?

Asked by
Irvene 5
10 years ago

So with functions, they are like my weakness in scripting. I'd like some help, because someone "fixed" my teaming function, but it's still not properly running

WHAT IT'S DOING: What the script is doing, is just saying "Welcome", counting down, then skipping the teaming going straight to the gametime. It doesn't do anything but that. No respawning, or anything. I'd like some help please.

THE SCRIPT: Here is the script;

BlueTeam = game.Teams["BlueTeam"]:Clone()
local RedTeam = game.Teams["RedTeam"]:Clone()
local NumPlayers = 2

repeat wait(0)until game:FindFirstChild("Teams")
local GameTime = 250 --5 minutes
local A = game:service('Players')
local A1 = "This service is unavailable, please wait until 1 more player joins..."
local A2 = "Welcome to the official meadows sfing game"
local A4 = "5"
local A3 = "The amount of players is successful, and the game will be starting in" .. A4 .. "Seconds.."
local A5 = "Teaming players..."
local A6 = "Blue team has won"
local A7 = "Red team has won"
local redplayers = 0
local blueplayers = 0
local M = Instance.new("Message",game.Workspace)
local H = Instance.new("Hint",game.Workspace)
local TimeForLoop = .5
local players = 0
local GameRun = false
local GameOver = false
local RegenTeams = false

function teams() -- THIS IS THE FUNCTION THAT DOESNT WORK!
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")},
true
);

function checkSpectators()
spectators = 0
for _, player in pairs(game:service('Players'):GetChildren()) do
if player.TeamColor == game.Teams.Spectators.TeamColor then
spectators = spectators + 1 end
if(spectators >= NumPlayers) then
game.Teams.BlueTeam:remove()
game.Teams.RedTeam:remove()
end

wait(3)
end
end
end

function findwinner()
if GameOver then
for _, player in pairs(game.Players:GetPlayers()) do
if player.TeamColor == BlueTeam.TeamColor then
players = blueplayers + 1
wait(5)
if blueplayers == 0 then
print("Blue team has lost")
M.Text = A7
elseif player.TeamColor == RedTeam.TeamColor then
players = redplayers + 1
if redplayers == 0 then
print("Red team has lost")
M.Text = A6
end
end
end
end
end
end

function RegenPlrs()
for i,v in pairs(game.Players:GetPlayers())do
if v and v.Character then
v.Character:BreakJoints()
end
end
end

function StartGame()
if GameRun then
teams()
RegenPlrs()
checkspectators()
findwinner()
end
end

coroutine.resume(coroutine.create(function()
while wait(TimeForLoop)do
if not ( #A:GetPlayers() >= NumPlayers ) then
M.Text = A1
else
StartGame()
M.Text = ""
wait(1)
M.Text = A2
wait(5)
M.Text = A3
for z = 5, 0, -1 do
M.Text = ""..z
wait(1)
end
for i = GameTime, 0, -1 do
H.Text = "Time left: "..i
wait(1)
end
wait(2)
M.Text = "Times up!"
wait(2)
M.Text = "Starting new round..."
wait(2.5)
end
end
end))

1 answer

Log in to vote
0
Answered by
Nifer2 174
10 years ago

Let's take a look at the function that you say doesn't work. I'm not sure if this is right, but...

function teams() -- THIS IS THE FUNCTION THAT DOESNT WORK!
    function balanceTeams(players, teams, randomize, callback) -- Instead of saying local balanceTeams = function(), do this.
        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
end; -- Needed one more end. Fixed for ya. Also, no need for the semicolon. :P

balanceTeams(
players:GetPlayers(), -- List of all players
{BrickColor.new("Bright red"), BrickColor.new("Bright blue")},
true -- You only have 3 parameters when you need four. Is this randomize or callback. Whichever it is, the other needs to be typed in.
);

I'm not sure if there are any other problems. Just fix what I wrote in the comments if I hadn't fixed it.

Ad

Answer this question