Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Select one random player? [ERROR]

Asked by
Hybric 271 Moderation Voter
10 years ago

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?

1
Why is this tagged trigonometry, functons and localscripts? They are all irrelevant. Lacryma 548 — 10y
0
@Duel there tags, Ik I didnt put tan() cos() and sin() Hybric 271 — 10y

4 answers

Log in to vote
0
Answered by 10 years ago
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!

Ad
Log in to vote
0
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
10 years ago

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
Log in to vote
-2
Answered by
Lacryma 548 Moderation Voter
10 years ago
local players = game.Players:GetPlayers()

local player = players[math.random(1, #players)]
player.Character:BreakJoints()
0
Whoever downvoted is jealous I got it first. Lacryma 548 — 10y
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. BlueTaslem 18071 — 10y
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 BlueTaslem 18071 — 10y
Log in to vote
-4
Answered by
KAAK82 16
10 years ago
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...

0
I'll try it, plus it has to be "local" Hybric 271 — 10y
0
k, try it, if doesn't work, tell me... I might have mixed up KAAK82 16 — 10y
0
also, I think I can Remember how to make a Picker with chance stats in it... KAAK82 16 — 10y
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. BlueTaslem 18071 — 10y
0
k fine! check my explenation no! KAAK82 16 — 10y

Answer this question