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

Why am I getting back a nil player in this random number generator?


local Players = game:GetService("Players")
local playerList = Players:GetPlayers()

wait(95)

local random = math.random(1, #game.Players:GetPlayers())
print(random)
randomplayer = playerList[random]
wait(5)
print(randomplayer)
local text = script.Parent
text.Text = (randomplayer.Name.." is on today's menu")



--It works here though, so I am really confused...:


local Players = game:GetService("Players")
local playerList = Players:GetPlayers()
wait(25)

script.Parent.ImageLabel.Visible = true
script.Parent.TextLabel.Visible = true
script.Parent.TextLabel.Text = "Guys, I think we are trapped in here now!"
local random = math.random(1, #game.Players:GetPlayers())
randomplayer = playerList[random]
wait()
print(randomplayer)
script.Parent.ImageLabel.Image = "http://www.roblox.com/Thumbs/Avatar.ashx?x=100&y=100&Format=Png&username="..randomplayer.Name
wait(10)
script.Parent.ImageLabel.Visible = false
script.Parent.TextLabel.Visible = false
script.Parent.TextLabel.Text = ""

Can someone explain to me what I am doing wrong in this gui code? It is generating the random number, but saying the player is nil.. This code works in one script, but not another, so I am really confused. I have stared at this code until I am going blind. I am sure it is something obvious and I am not seeing it. LOL

6 comments

0

Please put your code in a code block

0

^

0

^

1

Oh thank you InfinityDesign! Duh LOL

0

Does help if you mark the answer as accepted. I get clout & anyone with the same problem in the future has an easier way of seeing how to solve it. <3

0

Oh okay, I did, sorry, that was my first post in here. I didn't know.

3 answers

✓ Accepted answer

Your playerList array is 25 seconds older than the array you length check to get the random.

It's also possible GetPlayers returns a dictionary instead of an array; the documentation is unclear but the examples only iterate with pairs().

3 comments

0

Yeah, but that part of the code is working, it is the first part that is returning the nil player...

0

Because, as he said, PlayerList is old. You call :GetPlayers() right away and then wait 95 seconds. Most likely, PlayerList contains an array of just 1 player

0

That won't cause an issue. Calling the playerList will only affect the index # range later, it will not cause problems as it cannot index nothing; there would have to be no players in the server (This is obvious enough)

When you first run the game, it'll get the variable PlayersList as empty, because the code runs before the game starts. That's why it gives you the error that there is an empty value.

To fix this, wait for the game to load in, so wait about 2.5 seconds to 5 seconds for the game to load in.

After that you then want to get the number of players.

Another way to do this is, if a player is added to players, then add a value to a variable called numberList.

Example:

ServerScript

local numberList = 0

game:GetService("Players").PlayerAdded:Connect(function()
	numberList = numberList + 1
end)

game:GetService("Players").PlayerRemoving:Connect(function()
	numberList = numberList - 1
end)

Here, it starts at 0 because there are no players in the game. If a player joins it goes up by 1, if a player leaves it goes down by 1.

Then you can run the rest of your code after you wait a little bit to run the rest of it.

Rest of code:

local numberList = 0
local playerTable = {}

game:GetService("Players").PlayerAdded:Connect(function(player)
	numberList = numberList + 1
	table.insert(playerTable,player.Name)
	print(numberList)
end)

game:GetService("Players").PlayerRemoving:Connect(function(player)
	numberList = numberList - 1
	local tablePos = table.find(playerTable,player.Name)
	wait(0.25)
	table.remove(playerTable,tablePos)
	print(numberList)
end)

wait(5)

local randomNum = math.random(1,#game.Players:GetChildren())
print(randomNum)
local randomPlayer = playerTable[randomNum]
print(randomPlayer)
local text = script.Parent
text.Text = (randomPlayer.." Is On Today's Menu!")

--// Rest of Your Code After This Line

----------------------------------------------------------------------------

This might be more complicated than what you are asking, but I tested it out and this works perfectly fine.

Hope this helped!

If this is what you are looking for, don't forget to select this as the answer so other people know this question has been solved! If it didn't work, comment down below what went wrong and I'll be glad to help. If you have anymore questions, comment them below too and I'll try to answer them as best as I can.

~killerbrenden

Well, it seems that you're looking to get a player by the name of "1." Because of this, it returns nil because it's telling you that there isn't a player by that name.

Instead, you could try:

--[[
	Based off of your previous code.  It's just missing the second half. :)
]]--
local Players = game:GetService("Players")
local playerList = Players:GetPlayers()

wait(95)

local random = math.random(1, #game.Players:GetPlayers())
print(random)
local randomplayer
for i, player in pairs(playerList) do
	if i == random then
		randomplayer = player
	end
end
wait(5)
print(randomplayer.Name)
local text = script.Parent
text.Text = (randomplayer.Name.." is on today's menu")

This will get the player's metaphorical number, and then check if it is the same as the randomized number. This can be used in many other circumstances as well.

Hope this helped!

EDIT: Just a quick tip that'll help you get the player's UserThumbnail more efficiently.

Roblox actually has a function of the Players section in games that gets the user's thumbnail. Here's how you use it:

--[[
	Let's say you want to give a preview image of...as you say, the player "on today's menu."
]]--
local Players = game.Players
local PlayersList = players:GetPlayers()

wait(95)

local random = math.random(1, #game.Players:GetPlayers())
print(random)
local randomplayer
for i, player in pairs(playerList) do
	if i == random then
		randomplayer = player
	end
end
wait(5)
print(randomplayer.Name)
local image = script.Parent.Parent.ImageLabel
local image, isReady = players:GetUserThumbnailAsync(randomplayer.UserId, Enum.ThumbnailType.AvatarBust, Enum.ThumbnailSize.Size420x420)
image.Image = image
local text = script.Parent
text.Text = (randomplayer.Name.." is on today's menu")

Thought I'd just give you an easier-to-comprehend way of doing this.

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