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

Select one random player? [ERROR]

Well I want to make a script that selects one player to be a something, like chooses a random person. When I try to do it, it selects all people..

for i, player in pairs(game.Players:GetPlayers()) do
player.Character:BreakJoints()
end

What makes it one player instead of all?

2 comments

1

Why is this tagged trigonometry, functons and localscripts? They are all irrelevant.

0

@Duel there tags, Ik I didnt put tan() cos() and sin()

4 answers

local Players = game.Players:GetPlayers() --This returns a table containing all the players in the server
local RandomPlayer = Players[math.random(1,#Players)] --This picks a random player from that table
RandomPlayer.Character:BreakJoint() --This kills that random player

Hope this helped!

This problem does not require a loop. Since there's only one player you want to affect, there's no need to go over the whole thing (probably).

To select something randomly from a list (the list we will be using is GetPlayers) we have to generate a random index from the list (1, 2, 3, 4, ... , length of list - 1, length of list). We do that with this simple line:

local randomIndex = math.random(1 , #list);

Then, to get the random object from the list, we just have to ask for the object at that index:

local randomObject = list[randomIndex];

For getting a random player, it would look like this:

local allPlayers = game:GetService("Players"):GetPlayers();
if #allPlayers >= 1 then
	-- Selecting a random index from a length 0 list
	-- will result in an error, so we can only do
	-- something if the list has at least one thing
	local randomPlayerIndex = math.random(1,#allPlayers);
	local randomPlayer = allPlayers[randomPlayerIndex];
	if randomPlayer.Character then
		randomPlayer.Character:BreakJoints();
	end
end
local players = game.Players:GetPlayers()

local player = players[math.random(1, #players)]
player.Character:BreakJoints()

3 comments

0

Whoever downvoted is jealous I got it first.

0

I say this over and over. If you're going to answer, explain. Posting a script is useless for future readers and doesn't teach anything. So the next time someone needs something extremely similar, they just have to ask the question again.

1

Your answer is useless. Points aren't for correctness or for wrath. They are to encourage GOOD answers. NOT fast ones. Your answer is useless. "First", your answer was literally a minute faster than my second edit and your answer is useless. Seriously. I want GOOD answers on this site so that people can switch from learning Lua to actually using it. Don't give an answer if it's not a good one

plrs = game.Players:GetPlayers() --gets all Players and sets them to plrs

person = math.random(1, #plrs) --1 means it picks 1 out of some place, #plrs tells the system to pick 1 out of the plrs that is defined above

or something like it...

5 comments

0

I'll try it, plus it has to be "local"

0

k, try it, if doesn't work, tell me... I might have mixed up

0

also, I think I can Remember how to make a Picker with chance stats in it...

1

Your explanation is simultaneously incorrect and does not explain its process, so it cannot be used to learn from in any way. You shouldn't post something only done half way.

0

k fine! check my explenation no!

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