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

Getting a random player?

Asked by 8 years ago

I need help with tables and math.random. I want to create a script that choose a random player at a given time. Also I want to get the random player's name and give tools to that random player. Also when i get a random player it prints out table:02342DSF <-- that was made up but things like that get printed in the output.

Here's what I got:

local players = game.Players:GetChildren()
local name = players.Name
local randomplayer = {}
local x
game.Players.PlayerAdded:connect(function()
    local players = game.Players:GetPlayers()
    table.insert(randomplayer,players)
    while wait(3) do
        x = math.random(#randomplayer)
        print(randomplayer[x])
    end
end)

0
Well yes, because thats the value it holds in the table. Try to print this: print(randomplayer[x].Name) AmericanStripes 610 — 8y
0
It prints "nil" GeezuzFusion 200 — 8y
0
Mk. Give me a few minutes. AmericanStripes 610 — 8y

1 answer

Log in to vote
2
Answered by 8 years ago

The best thing to do in a case like this is to just start from the beginning.


I love functions.


They are awesome. They help organize your code and stops repetitive code. So let's start with creating a function along with creating a table to hold the players. However, I think an error may stem with holding a player type in a table, so I will just store their names there.

function RandomPlayer()
    local holdPlayers = {}
end

Now let's go ahead and create a for loop to go through all of the players and add their names into the table.

function RandomPlayer()
    local holdPlayers = {}

    for _,v in pairs(game.Players:GetPlayers()) do
        table.insert(holdPlayers, v.Name)
    end

end

Alright. Now for the "hard" part. We will create a variable of a random number, much like you already did. We will also add the variable that holds the random player's name from the table.

function RandomPlayer()
    local holdPlayers = {}

    for _,v in pairs(game.Players:GetPlayers()) do
        table.insert(holdPlayers, v.Name)
    end

    local randomNum = math.random(1, #holdPlayers)
    local randomPlayerName = holdPlayers[randomNum]
end

Now to wrap it up is easy. We will use FindFirstChild to find the player, and then return it.

function RandomPlayer()
    local holdPlayers = {}

    for _,v in pairs(game.Players:GetPlayers()) do
        table.insert(holdPlayers, v.Name)
    end

    local randomNum = math.random(1, #holdPlayers)
    local randomPlayerName = holdPlayers[randomNum]
    local randomPlayer = game.Players:FindFirstChild(randomPlayerName)
    if randomPlayer then
        return randomPlayer
    else
        return
    end
end

This has not been tested to work. I simply did all coding in the code block. Please be aware of any basic spelling errors in the script itself.

Here is the breakdown of a for loop: Click here

The "_" is a variable that holds the 'key.' I did not have to use the 'key' variable so usually I just put an _ when I don't need it.

0
I liked that you took your time to help me. Now im going to upvote every post youve answered or asked. Thanks Dude! GeezuzFusion 200 — 8y
0
One more thing, What does that Underscore mean? Does it do anything or it's just a variable? GeezuzFusion 200 — 8y
1
I have updated my answer with an explanation AmericanStripes 610 — 8y
0
It's basically what number the player was in game.Players. AmericanStripes 610 — 8y
View all comments (2 more)
0
A better way to handle this is just use `holdPlayers = game.Players:GetPlayers()` rather than store the player's names; that way, it has more functionality. To get a random player, just do: randomPlayer = holdPlayers[math.random(1, #holdPlayers)]; Then, to get the name: randomPlayerName = randomPlayer.Name; We get to cut out the for loop, the randomNum variable, and the if statement - optimised! PowerHotmail123 90 — 8y
0
Yes, however before I jumped to optimising I wanted him to fully understand what was happening. I wanted to teach him how, not just what. However you are very correct! That is a very nice way of optimizing the code! AmericanStripes 610 — 8y
Ad

Answer this question