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

Why is this random 1 person team selector not working?

Asked by 6 years ago

How do i make , so that a script selects a random player, puts it in the team "Monster" and displays a message that he is the monster. tried this - didnt work.

TeamName = "Monster"
players = {}
selected = ""

function GetPlayers()
    for _,v in pairs(game.Players:GetChildren()) do
        table.insert(players,v)
    end
end

GetPlayers()

function SelectPlayer()
    local num = math.random(1, #players)
    selected = players[num]
    print('Selected player was: ' ..selected)
end

SelectPlayer()

function TeamPlayer(player)
    local play = game.Players:FindFirstChild(player)
    local team = game.Teams:FindFirstChild(TeamName)

    if play~= nil then
        play.TeamColor = team.TeamColor
    end
end

TeamPlayer(selected)

4 answers

Log in to vote
1
Answered by
UgOsMiLy 1074 Moderation Voter
6 years ago
Edited 6 years ago

Firstly, GetPlayers is literally the name of a method of Players. game.Players:GetPlayers(). Use that instead. Secondly, FindFirstChild takes the name of a string. The player parameter is already the player, not only would FindFirstChild(player) not work, but FindFirstChild(player.Name) and player are the same thing, so use the latter.

And, I'm pretty sure that the previous answers should work, but if not then here you go:

TeamName = "Monster"
players = {}

function TeamPlayer(player)
    local team = game.Teams:FindFirstChild(TeamName)

    if player~= nil then
        player.TeamColor = team.TeamColor
    end
end

function SelectPlayer()
    local plrs = game.Players:GetPlayers()
    if #plrs < 1 then return end
    local selected = plrs[math.random(1, #plrs)]
    if selected then
        print('Selected player was: ' ..selected.Name)
        TeamPlayer(selected)
    end
end

SelectPlayer()

0
Seems like Selected is blue underlines, it is not defined. How can i fix that? UncleBenss -3 — 6y
0
And it prints out nothing. No errors UncleBenss -3 — 6y
0
Thats because it was supposed to be "local" selected. My bad, but I've edited that. UgOsMiLy 1074 — 6y
Ad
Log in to vote
0
Answered by
mattscy 3725 Moderation Voter Community Moderator
6 years ago
Edited 6 years ago
local teamName = "Monster"
local plrs = game.Players:GetPlayers()
if #plrs > 0 then
    local chosenPlr = plrs[math.random(1,#plrs)]
    chosenPlr.Team = game:GetService("Teams")[teamName]
end
0
Receiving this error ;/ 15:29:38.952 - ServerScriptService.Monster Chooser:4: bad argument #2 to 'random' (interval is empty) 15:29:38.953 - Stack Begin UncleBenss -3 — 6y
0
If you do it on game start, there wont be any players to choose from in the game because no one would have joined yet. mattscy 3725 — 6y
0
Im a beginner scripter, i need to put your codes on the top of mine? Or do i need to publish the game to see if it chose or no UncleBenss -3 — 6y
Log in to vote
0
Answered by 6 years ago
Edited 6 years ago

Here you go, I added the line:

repeat wait() until #game.Players:GetPlayers() >= 1

this waits until there is an actual player in the game to run this function on. I created a function to encompass this scope so none of the code runs unless there is one or more players in the game.

TeamName = "Monster"
players = {}
selected = ""
function setRandomTeam()
    repeat wait() until #game.Players:GetPlayers() >= 1
    function GetPlayers()
        for _,v in pairs(game.Players:GetChildren()) do
            table.insert(players,v)
        end
    end

    GetPlayers()

    function SelectPlayer()
        local num = math.random(1, #players)
        selected = players[num]
        print('Selected player was: ' ..selected)
    end

    SelectPlayer()

    function TeamPlayer(player)
        local play = game.Players:FindFirstChild(player)
        local team = game.Teams:FindFirstChild(TeamName)

        if play~= nil then
            play.TeamColor = team.TeamColor
        end
    end

    TeamPlayer(selected)
end
setRandomTeam()
0
GetPlayer SelectPlayer TeamPlayer is not defined. It has blue underlines UncleBenss -3 — 6y
Log in to vote
0
Answered by 6 years ago

Still need help!

Answer this question