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

How do I make a team from a script and assign the player that team upon entering the game?

Asked by 7 years ago

I'm trying to make a script so when a player enters the game; 1. A team will be created. 2. The team's name will be the player's username. 3. The colour will be random. 4. Assign the player to that team.

And also it should delete the team the player was on when the player leaves. When I test it, the player joins 'Neutral'. I have a spawn there so the player spawns where I want it. When I take off neutral on the spawn, the spawn 'doesn't exist' anymore (the player doesn't spawn there)

This is what I have so far;

game.Players.PlayerAdded:connect(function(player)
    local team = Instance.new('Team')
    team.Parent = game.Teams
    team.Name = ""..player.Name..""
    team.TeamColor = BrickColor.Random()
    player.TeamColor = BrickColor.new(team.TeamColor)
end)

I'm sorry for the long question/description. Answers are greatly appreciated! :)

1 answer

Log in to vote
0
Answered by
H4X0MSYT 536 Moderation Voter
7 years ago
function Added(Player)
    local Team = Instance.new("Team") -- create a tea
    Team.Parent = game.Teams
    Team.Name = Player .. [['s team]]
    local Attempt = BrickColor3.random -- chose a random color
    for i,v in pairs(game.Teams:GetChildren()) do
        if v.TeamColor == Attempt then -- if the color has allready been used
            while true do() --                   we keep making new colors until we get one thats not used
                Attempt = BrickColor3.random
                    if not Attempt == v.TeamColor then break end
            end
        Team.TeamColor = Attempt -- finalise team color
        Player.TeamColor = BrickColor3.new(Team.TeamColor) -- set players team color
    end
end

function Leave(Player)
    for i,v in pairs(game.Teams:GetChildren()) do
        if v.Name =Player .. [['s team]] then -- find the players team
            v:Remove() --                              and delete it
        end
    end         
end
game.Players.PlayerLeaving:connect(Leave)
game.Players.PlayerAdded:connect(Added)
0
Did some fixes and it's coming up with an error. Moshipikachu123456 15 — 7y
Ad

Answer this question