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)
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()
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
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()