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

Argument 1 missing or nil in table?

I am on the verge of making my own player list and got almost got it to work. The local script is:

local PlayerService = game:GetService("Players")
local players = game.Players:GetChildren()
local ImageLabel = script.Parent
while true do
	wait()
		local PlayerName = players[2]
		print(PlayerName)
		local PlayerId = PlayerService:GetUserIdFromNameAsync(PlayerName)
		print(PlayerId)
		local ThumbType = Enum.ThumbnailType.AvatarBust
		local ThumbSize = Enum.ThumbnailSize.Size420x420
		local Content, IsReady = PlayerService:GetUserThumbnailAsync(PlayerId, ThumbType, ThumbSize)

		ImageLabel.Image = Content
	
end

This gets the second player and grabs their user id from their name. However, it fails on line 8 and gives me the error

  23:09:23.443 - Argument 1 missing or nil

What's wrong with this?

2 answers

✓ Accepted answer

This error occurs when specific function aren't called with enough arguments. First of all the if the error gives a line number, backtrack it. The error states argument 1 missing or nil thus, the first argument PlayerName must be nil since it's the first argument. Since variable PlayerName is nil, the only declaration made with that variable name is on line 6. Therefore my theory is that since variable players is an array made by gathering the players, this error would appear when you are playing alone, thus the second index on that array would be nil.

I suggest rewriting the script so that the loop will run once the game has enough players or make an if statement if there's one player. Samples:

To wait for two players

repeat
	wait()
until #game.Players:GetPlayers()>=2

To check if there is a second player using an if statement.

local PlayerName = players[2]
if not PlayerName then
	PlayerName = players[1]
end

To get a random player that is not yourself

local Players = game.Players:GetPlayers()
local Index_You = table.find(Players,game.Players.LocalPlayer)
table.remove(Players,Index_You)

local a = Players[math.random(1,#Players)] -- random player, but it can't be you.

Sorry if it's vague, it's been too long since I've been here.

19 comments

0

I'll give it a shot

0

It works for player 2 but not player 1

0

Which method?

0

The repeat wait() until one

0

o sorry lemme reedit i made a typo

1

Ok so that happens because when the second player joins, it adds to the array, so it will always become the second player

0

so how can I battle this?

0

i got an idea but lemme ask you one thing, does it matter if its the second player or any player?

0

Any player for all the gui's (it's a playerlist [it changes])

0

so any random player will work?

0

I need to get all the players and put them in these guis (so the order doesn't matter just that there is no duplicates and all are included)

0

Basically, you want the script so that the player would see the other player's profile picture?

0

Absolutely!

0

what if theres three players? or more?

0

I'm trying to make these local scripts in the gui so it replaces an image for the every player's number under a picture(there is several)

0

so any random other player that is not yourself?

0

yes

0

ok first of all, you need to put alot of details to your question because this comment section is wayyyy too long, second of all, I'll re-edit the answer so it has what you need.

1

ok go check

Finally came to this thanks to SoftlockedUnderZero!

local players = game.Players
local ImageLabel = game.Players.LocalPlayer.PlayerGui.Queue.PlayerBoxes.Player2
while wait() do
	for i, v in pairs(players:GetChildren()) do
	wait()

		local PlayerName = players:GetChildren()[2].Name
local PlayerId = players:GetUserIdFromNameAsync(players:GetChildren()[2].Name)
print(PlayerId)
		local ThumbType = Enum.ThumbnailType.AvatarBust
		local ThumbSize = Enum.ThumbnailSize.Size420x420
		local Content, IsReady = players:GetUserThumbnailAsync(PlayerId, ThumbType, ThumbSize)
		ImageLabel.Image = Content
		end
end

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