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

How to select random player?

Hi there! I'm trying to create a game where one player is selected to kill the other players anonymously. How would you do this? I've created a script already but it hasn't worked.

function randomizer()
	local players = game.Players:GetChildren()
	local rPlayer = game.Players:GetPlayers()
	[math.random(1, #players)]
	wait(5)
	rPlayer.TeamColor = BrickColor.new("Really red")
end

randomizer()

The error message reads

15:25:18.309 - ServerScriptService.Selecter:4: bad argument #2 to 'random' (interval is empty)

What does this mean? Is something wrong with the script?

1 answer

✓ Accepted answer
function randomizer()
	local players = game:GetService(:Players"):GetPlayers()
	print(players[math.random(1, #players)])
end

while wait(1) do
	randomizer()
end

Here is another way, you can obviously change the print to whatever you want! Here is an example what you can do!

function randomizer()
	local players = game:GetService(:Players"):GetPlayers()
	local i = (players[math.random(1, #players)])
	--now the random player is located as i, do whatever you want with it!
	i:Kick("hello!") --example
end

while wait(1) do
	randomizer()
end

3 comments

0

Thanks! Is there just a way to keep it from running all the time, like if someone is alreay on the "Really red" brickColor team?

0

That is entirely up to you, and should be implemented into the basis of your game. Before each round begins you'll need to loop through all the players and choose one randomly to be the murderer.

0

You should use the Random library instead of math.random, no need to seed and not biased.

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