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

Problem with splitting players into two teams?

Asked by 4 years ago

I got all the "plrs" in a table and then tried to split them into two. But it doesnt place the players on each team..

plrs = IsOrNotAfkTable(false)

local RedTeam = Instance.new("Team", game:GetService("Teams"))
RedTeam.TeamColor = BrickColor.new("Really red")
RedTeam.Name = "Red"
local Folder = Instance.new("Folder", RedTeam)
Folder.Name = "leaderstats"
local Hits = Instance.new("IntValue",Folder)
Hits.Name = "Hits"


local BlueTeam = Instance.new("Team", game:GetService("Teams"))
BlueTeam.TeamColor = BrickColor.new("Navy blue")
BlueTeam.Name = "Blue"
local Folder = Instance.new("Folder", BlueTeam)
Folder.Name = "leaderstats"
local Hits = Instance.new("IntValue",Folder)
Hits.Name = "Hits"

local redTeam = {}
local blueTeam = {}

local plrsOnEachTeam = math.floor(#plrs / 2)

function Teamer()
for i = 1,plrsOnEachTeam,1 do
    table.insert(redTeam,#redTeam + 1,plrs[1])
    table.remove(plrs,1)
end

for i =1,plrsOnEachTeam,1 do
    table.insert(blueTeam,#blueTeam + 1,plrs[1])
    table.remove(plrs,1)
end

--In case there is a leftover player
if #plrs == 1 then
    table.insert(redTeam,#redTeam + 1,plrs[1])
    table.remove(plrs,1)
end

1 answer

Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

First of all, you never called the function. Second of all, you never actually added each player, all this script is doing is deciding how many players should go on each team, but it never actually sets it. This is the script I came up with

wait(5)

plrs = IsOrNotAfkTable(false)

local RedTeam = Instance.new("Team", game:GetService("Teams"))
RedTeam.TeamColor = BrickColor.new("Really red")
RedTeam.Name = "Red"
local Folder = Instance.new("Folder", RedTeam)
Folder.Name = "leaderstats"
local Hits = Instance.new("IntValue",Folder)
Hits.Name = "Hits"


local BlueTeam = Instance.new("Team", game:GetService("Teams"))
BlueTeam.TeamColor = BrickColor.new("Navy blue")
BlueTeam.Name = "Blue"
local Folder = Instance.new("Folder", BlueTeam)
Folder.Name = "leaderstats"
local Hits = Instance.new("IntValue",Folder)
Hits.Name = "Hits"

local redTeam = 0
local blueTeam = 0

local plrsOnEachTeam = math.floor(#plrs / 2) 

function Teamer()
    for i = 1,plrsOnEachTeam,1 do
        redTeam = redTeam + 1
        table.remove(plrs,1)
    end

    for i = 1,plrsOnEachTeam,1 do
        blueTeam = blueTeam + 1
        table.remove(plrs,1)
    end

    --In case there is a leftover player
    if #plrs == 1 then
        redTeam = redTeam + 1
        table.remove(plrs,1)
    end

    local redLeft = redTeam
    local blueLeft = blueTeam

    for i, v in pairs(game.Players:GetPlayers()) do
        if v then
            if redLeft > 0 then
                redLeft = redLeft - 1
                v.Team = RedTeam
            elseif blueLeft > 0 then
                blueLeft = blueLeft - 1
                v.Team = BlueTeam
            end
        end
    end
end

Teamer()
Ad

Answer this question