Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Error: "Attempt to index nil with UserId" How do I fix this?

Asked by 3 years ago
Edited 3 years ago

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.

2 answers

Log in to vote
0
Answered by
DevingDev 346 Moderation Voter
3 years ago

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)
0
Thank you so much DevingDev, this worked! StirSavvy 38 — 3y
Ad
Log in to vote
0
Answered by 3 years ago

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)
0
Are you sure? The error is on line 3 and 11, where UserId is mentioned. StirSavvy 38 — 3y

Answer this question