How to run this without getting error prompt

So before anything the code works. If I remove the UserId part then it does not. So I do not know why it prompts a error when it actually works

The script is a local script!
Error
Players.3DReality.PlayerGui.bk.MenuGuiHandler:585: attempt to index nil with ‘UserId’ - Client - MenuGuiHandler:585

Code

local ImageSize = Enum.ThumbnailSize.Size420x420
				local ImageType = Enum.ThumbnailType.HeadShot 
				
				local players = game.Players
				local PlayerToGrab = players:FindFirstChild(Server:GetAttribute(Plr.Name))
			
local PlayerImage = game.Players:GetUserThumbnailAsync(PlayerToGrab.UserId, ImageType, ImageSize)

The line that is erroring but not actually erroring

local PlayerImage = game.Players:GetUserThumbnailAsync(PlayerToGrab.UserId, ImageType, ImageSize)

Like I said the code works fine but its prompting a error in the output even though its proving the picture and im just trying to keep the output clean

2 Likes

What do you see when you print:

print("Player = ", PlayerToGrab)
print("Type = ", ImageType)
print("Size = ", ImageSize)

local PlayerImage = game.Players:GetUserThumbnailAsync(PlayerToGrab.UserId, ImageType, ImageSize)
3 Likes

To clarify, PlayerToGrab is not nil?

3 Likes

3 Likes

Correct. Like I mentioned it works. It grabs the players image and places it. Its just prompting a error for unkown reasons.

The code is to get the players pictures for a lobby system im working on and as you can see it it works
LobbyPlayerImage

1 Like

This is the line that is not working:

local PlayerToGrab = players:FindFirstChild(Server:GetAttribute(Plr.Name))

This line is producing a nil value.

2 Likes

You may need to use a WaitForChild or look closer at Server:GetAttribute(Plr.Name)

In fact, just print(Server:GetAttribute(Plr.Name)) and see if you get nil.

1 Like

PrintCommand
PrintProof1

1 Like

The Full Function if that might help. Like I stated it works but just roblox says it erroring when it not lol. Like everything works im just trying to stop a false error prompt

function LobbyEvent(Type, Server)

	
	ServerLeaverUI.TextButton.Activated:Connect(function()
		MainScreenUI.BackGround.Visible = true
		MainMenu.Visible = true
		
		ServerAdjustmentEvent:FireServer(Server)
		
	end)

	if Type == "Update" then
		LobbySurfaceUI.BackGround.MapBackGround.Image = MapImages:FindFirstChild(Server:GetAttribute("MapName")).Texture
		
		for i, Plr in pairs( LobbySurfaceUI.BackGround:GetChildren()) do
			
			if Server:GetAttribute(Plr.Name) then
				local ImageSize = Enum.ThumbnailSize.Size420x420 -- Thumbnail Size
				local ImageType = Enum.ThumbnailType.HeadShot -- Thumbnail Type
				
				local players = game.Players
				
				local PlayerToGrab = players:FindFirstChild(Server:GetAttribute(Plr.Name))
			
				print(Server:GetAttribute(Plr.Name)) 
				 
				local PlayerImage = game.Players:GetUserThumbnailAsync(PlayerToGrab.UserId, ImageType, ImageSize) -- Gets Image from UserId
				
				
				print("Player = ", PlayerToGrab)
				print("Type = ", ImageType)
				print("Size = ", ImageSize)

				
				

				Plr.Image = PlayerImage
				WarTableBillbord.PlayerCounter.Text = (Server:GetAttribute("CurrentPlayers").."/"..Server:GetAttribute("MaxPlayers"))
				Server.AttributeChanged:Connect(function()
					LobbyEvent(Type, Server)
				end)
			end
		end
	end
	
end

What is in the output window when you:

print("Attriburte =", Server:GetAttribute(Plr.Name))

1 Like

AttributePrint

1 Like

Well, how about:

local PlayerToGrab = players:FindFirstChild(Server:GetAttribute(Plr.Name))

print("Class = ", PlayerToGrab.ClassName)
1 Like


ClassPrint

1 Like

I actually just noticed its doing the same thing

Every piece of the statement has been checked and seems to be valid.

I am not sure why it errors when you try and add UserId to the PlayerToGrab since you just showed that PlayerToGrab is a Player.

Does it error when you try to print("UserId =", PlayerToGrab.UserId)

See thats what I was thinking I spent a couple hours tryna figure out I think it might just be a bug? I am unsure. Like it works its not affecting the code its just prompting errors.


On the page for the GetUserThumbnailAsync the have it structured a little differently:

ocal content, isReady = Players:GetUserThumbnailAsync(userId, thumbType, thumbSize)

don’t know why or what it means, but it may solve your issue.

You called “FindFirstChild” with PlayerToGrab, but you never actually added a check to see if it’s an existing value inside of Players Service.

Simply add a “if PlayerToGrab then” right after the variable and see if this stops the error. You might have objects in that frame that aren’t actually a player that exists or you have called this function multiple times before said player exists, explaining why it works but errors at the same time.

Yeah that actually is covered from the server side.

Basically the code chain goes as followed

Player Joins/Creates Lobby – Fire to Server

Server Finds the Lobby/Creates it and – fires back to all player inside the lobby with the update

client then setups the images for the spot the players in and does everything else for the lobby update.

So the if statement

if Server:GetAttribute(Plr.Name) then 

Is the double checker

Its good I just switched the code to the server side and just called back for the stored picture and it seems to be working. with no issue.

I even tried doing the game:GetService(“Players”)
i dont know if the bug was from it being locally but idk i think im just getting robloxed

That’s only checking the attribute, not the “PlayerToGrab” variable. You called FindFirstChild but never actually checked it. You need to check it on the client so it doesn’t error for a nil value.

You need to check that “PlayerToGrab” was found otherwise it will return as nil, and you cannot index “UserId” with a nil value.

1 Like