I am making a script for a game in ROBLOX that I am working on that requires the player's thumbnail image to be shown on a GUI grid. I need to do this by getting the players UserId, but there is an error.
Script:
local PlayersFrame = script.Parent:WaitForChild("PlayersFrame") local rStorage = game:GetService("ReplicatedStorage") local rEvents = rStorage:WaitForChild("RemoteEvents") local plr = game.Players.LocalPlayer rEvents.AddPlayer.OnClientEvent:Connect(function(plr) local image = Instance.new("ImageLabel",PlayersFrame) image.Name = plr.UserId image.BorderSizePixel = 0 image.BackgroundTransparency = .65 image.BackgroundColor3 = Color3.fromRGB(0,0,0) image.Image = game.Players:GetUserThumbnailAsync(plr.UserId,Enum.ThumbnailType.HeadShot,Enum.ThumbnailSize.Size420x420 end)
The error is, "Attempt to index nil with 'UserId'" and it is on line 3 and 7 shown above. If someone knows how to fix this please comment.
The first argument in the RemoteEvent
OnClientEvent
is nil. It's name is also the same as the name you're using to define the LocalPlayer
.
Parenting as the second argument of Instance.new
is also deprecated.
local image = Instance.new("ImageLabel") image.Parent = playersFrame
Since I don't have the server side, I'll assume that you're not passing the player argument to the client. I assume you're only doing
remoteEvent:FireClient(player)
But this is wrong if you want to include player as an argument on the client. The first argument is the player you're sending the event to. If you want to send information about another player then you need to use the second argument for that.
If you're planning on doing it like this then you could just get rid of the player argument inside of the OnClientEvent
on the client.
remoteEvent:FireClient(player, player)
This should work. I've changed the plr
argument name to targetPlayer
.
-- Services local replicatedStorage = game:GetService("ReplicatedStorage") local playersService = game:GetService("Players") local remoteEvents = replicatedStorage:WaitForChild("RemoteEvents") -- Player local player = playersService.LocalPlayer local playersFrame = script.Parent:WaitForChild("PlayersFrame") remoteEvents.AddPlayer.OnClientEvent:Connect(function(targetPlayer) local image = Instance.new("ImageLabel") image.Parent = playersFrame image.Name = targetPlayer.UserId image.BorderSizePixel = 0 image.BackgroundTransparency = .65 image.BackgroundColor3 = Color3.fromRGB(0,0,0) image.Image = game.Players:GetUserThumbnailAsync(targetPlayer.UserId,Enum.ThumbnailType.HeadShot,Enum.ThumbnailSize.Size420x420) end)
Your mistake is so easy:Just add a dot here:
plr.UserId.image.BorderSizePixel = 0 image.BackgroundTransparency = .65 image.BackgroundColor3 = Color3.fromRGB(0,0,0)