#How do I make a team assigner?

1 messages · Page 1 of 1 (latest)

restive ruin
#

I scripted this code with not a lot of experience with tables and dictionaries, and I really need help 🙏 with what I did wrong.

local team = {["green"] = nil, ["red"] = nil, ["blue"] = nil}

local Team = function(character)
    for char, color in pairs(team) do
        if color == nil then 
            character = team[color]
            break end
    end
end
tranquil wigeon
#

worse

#

do u mean

#

you want to make

#

random team assigner?

restive ruin
#

yes

tranquil wigeon
#

ok

#

I saw lots of problem

#

remember

#

when using for pairs loop, the first parameter is always key and second parameter is always value

restive ruin
#

ok

tranquil wigeon
#

when using for ipairs loop, first parameter is always index and second parameter is always value

restive ruin
#

I thought it was the other way around

tranquil wigeon
#

lemme write better script for you

restive ruin
#

ok

tranquil wigeon
#

pairs can use on table and dictionary

#

ipairs can use on table ONLY, otherwise IT WON'T loop

restive ruin
#

I see

tranquil wigeon
#
local Teams = game:GetService("Teams") -- Get the service of Teams
local Players = game:GetService("Players") -- Get the service of Players

math.randomseed(tick()) -- set seed to random

local function PlayerAdded(Player : Player)
    local Available_Teams = Teams:GetTeams() -- Get all teams, this will return table
    local random_index = math.random(1,#Available_Teams)
    local selected_team = Available_Teams[random_index]
    
    Player.Team = selected_team
end

Players.PlayerAdded:Connect(PlayerAdded)

@restive ruin I hope you understand it, if you got problem please ask me and I will try my best to help you.

#

math.randomseed(tick()) is optional, you can remove it if you want