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

How do I make the game choose more than 1 random player?

So I want to make a script that makes the game pick 1+ player randomly and is not the same person the game chose before.

1 comment

1

good luck

2 answers

✓ Accepted answer

Make a list of all the players and randomly pick. After picking a player, remove them from the list

local PlayerList = {}

local function refresh()
	PlayerList = {}
	for _, plr in pairs(game.Players:GetChildren()) do --for every child (player) of the game.Players
		table.insert(PlayerList, plr.Name) --add the player's name to the list
	end
end

local function RandomPlayer()
	local num = math.random(1, #PlayerList) --get a random index number
	local player = PlayerList[num] --Get player with that index number
	for index, plr in pairs(PlayerList) do --Go through every player in the list
		if plr == player then --When the player is found...
			table.remove(PlayerList, index) --remove them from the list and...
			return game.Players[player] --return the player's instance
		end
	end
end

Here are the functions you need

2 comments

0

Well thanks but where do I put the script that the random players will do?

0

It depends on what you are doing. Usually for this stuff, you put a script in the ServerScriptService

To randomly pick a player, do:

local Players = game:GetService('Players')

local playerList = Players:GetPlayers()
local randomPlayer = playerList[math.random(1, #playerList)]

To make it not pick the previous player, store the previous player as a variable and compare it with the new player:

local Players = game:GetService('Players')

local previousPlayer

repeat
	local playerList = Players:GetPlayers()
	local randomPlayer = playerList[math.random(1, #playerList)]
until (previousPlayer ~= randomPlayer)

previousPlayer = randomPlayer

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