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?
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()
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...