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

Picking two random players?

I am making a game where two random players are picked and compete. Here is my script:

game.Players.PlayerAdded:connect(function(plyr)
local chosen=Instance.new("StringValue",plyr)
chosen.Name=("chosen")
chosen.Value=("Zero")
end)

local model=game.Players
player = model:GetChildren()[math.random(#model:GetChildren())]
if player.chosen.Value==("Zero") then
	player.chosen.Value=("One")
end
player = model:GetChildren()[math.random(#model:GetChildren())]
if player.chosen.Value==("Zero") then
	player.chosen.Value=("Two")
end

This script looks inefficient and most times it does not work for me. Is there a better way to pick random players?

3 comments

0

Unless you have more code somewhere, the script will run before anyone joins the game. Also, you should allow for if the same player is picked twice - don't want that.

0

Are there any errors? Also why are you using a string value? An IntValue will work with 0,1 and 2

0

What I'm asking for here is a way to not allow the same player to be picked twice

1 answer

Yes, there is a better way. I'd also recommend making a function for it so you may reuse the code at any given time in your script.

Tables

To prevent the random selection from picking the same player twice, we should use a table to store the information, and remove randomly selected players to avoid choosing them a second or third time.


For loop

Then we can use a for loop to iterate the Players service, and insert the randomly selected players into an array.


Example

And finally, and our example should look like this, where we implement the use of tables and for loops, to control what random player has been selected:

-- Players service
local Players = game:GetService("Players")

-- A function we can call to return a table of random players.
-- We can also pass an argument to decide how many random players
-- we want to return, which is what "amount" represents.

local function GetRandomPlayers(amount)
	-- Just a way to set a default "amount" value
	-- if we don't want to define it.
	amount = amount or 2

	-- Get all the players
	local players = Players:GetPlayers()

	-- If there are no players, stops the function here
	if #players == 0 then
		return "No players found"
	end

	-- Create the table we'll store random players in
	local chosen = {}

	-- Iterate 1 through whatever our amount is
	for i = 1,amount do

		-- Get the random number key
		local r = math.random(#players)

		-- Insert the random player from "players" into
		-- the "chosen" table.
		chosen[#chosen+1] = players[r]
		
		-- Remove that player from the list we're
		-- iterating, to avoid choosing it multiple times
		table.remove(players,r)

	-- End for loop
	end

	-- Return a tuple of random players
	return unpack(chosen)
end

-- And if we wanted to get the players, then we'd simply call the function:

-- Sets "Player1" and "Player2" to two different
-- random players
local Player1, Player2 = GetRandomPlayers()

-- Or, if we wanted to get more than two random players:

local Player1, Player2, Player3 = GetRandomPlayers(3) -- Remember, we made it so you can get more than just 2 random players if you choose to pass an argument to this function, representing how many random players you want.

Hope this helped, let me know if you have any questions.

1 comment

0

table.remove returns what is removed. Thus local player = table.remove(players, math.random(#players)) is a random player that has been removed from players

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