Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Why is this random 1 person team selector not working?

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

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

3 comments

0

Seems like Selected is blue underlines, it is not defined. How can i fix that?

0

And it prints out nothing. No errors

0

Thats because it was supposed to be "local" selected. My bad, but I've edited that.

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

3 comments

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

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.

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

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

1 comment

0

GetPlayer SelectPlayer TeamPlayer is not defined. It has blue underlines

This discussion is preserved as it was. New answers, comments, and votes are closed.