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

How to make a auto assign script which assigns to losing team?

Asked by 10 years ago

Hi. I'm attempting to create a script that auto assigns a new player to join the losing team. However, I don't fully know how to do this. I've done calculating the KDR of one player, but how to I work out the KDR of the WHOLE team instead of 1 player?

Here is what I done so far:

function checkteams()
    for i, v in pairs, game.Players:GetPlayers() do
        if v.TeamColor == "Bright blue" then
            kills = v.leaderstats.KOs.Value
            deaths = v.leaderstats.Wipeouts.Value
            kdr = (kills/deaths)
        if v.TeamColor == "Bright red" then
            kds = (kills/deaths)        
        end
        end
    end
end

game.Players.PlayerAdded:connect(function(plr)
    if Workspace.MapHolder:GetChildren()[1] then
        checkteams()
        if kds > kdr then
            plr.TeamColor = BrickColor.new("Bright red")
            v:LoadCharacter()
        elseif kdr>kds then
            plr.TeamColor = BrickColor.new("Bright blue")
            v:LoadCharacter()
        end
    end
end)

Thank you.

1 answer

Log in to vote
1
Answered by 10 years ago

This has a few comments explaining a few flaws that were in your script, as-well as additions to the code to make it more efficient and variables making a couple of services easier to access.

local Plrs = game:GetService("Players") -- Pre-defined for ease of access.
local Ws = game:GetService("Workspace")

function checkTeams()
    for _, v in pairs(Plrs:GetPlayers()) do -- You need the brackets, not comma
        if v.TeamColor == BrickColor.new("Bright blue") then -- TeamColor works with BrickColor, not strings
            local Kills = v.leaderstats.KOs.Value
            local Deaths = v.leaderstats.Wipeouts.Value
            Kdr = (Kills/Deaths)
        elseif v.TeamColor == BrickColor.new("Bright red") then
            local Kills = v.leaderstats.KOs.Value
            local Deaths = v.leaderstats.Wipeouts.Value
            Kds = (Kills/Deaths)
        end
    end
    return Kds,Kdr
end

Plrs.PlayerAdded:connect(function(plr)
    if Ws['MapHolder']:GetChildren()[1] then
        Kds,Kdr = checkTeams()
        if Kds > Kdr then
            plr.TeamColor = BrickColor.new("Bright red")
            plr:LoadCharacter() -- You're loading the plr's character, not v
        elseif Kdr > Kds then
            plr.TeamColor = BrickColor.new("Bright blue")
            plr:LoadCharacter()
        end
    end
end)

0
Oops, thank you. I got a bit confused with the for i,v in pairs since I now usually use for i,v in next. truefire2 75 — 10y
0
Heh, no problem. RaverKiller 668 — 10y
Ad

Answer this question