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

Getting a random player?

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)

3 comments

0

Well yes, because thats the value it holds in the table. Try to print this: print(randomplayer[x].Name)

0

It prints "nil"

0

Mk. Give me a few minutes.

1 answer

✓ Accepted answer

#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.

6 comments

0

I liked that you took your time to help me. Now im going to upvote every post youve answered or asked. Thanks Dude!

0

One more thing, What does that Underscore mean? Does it do anything or it's just a variable?

1

I have updated my answer with an explanation

0

It's basically what number the player was in game.Players.

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!

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!

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