Having trouble getting UserId

I have made a topic about his earlier but I wanted to make another one to show this specific problem.

So I have a game and you have to touch a part to win (haven’t worked on it much yet) and there is a sign in the lobby that shows you the last winner. I have a little bit of a script so far but it comes up with an error saying that UserId doesn’t exist. Please reply if you know or have any ideas on a solution.

Script:

local npc = script.Parent
local winner = game.ReplicatedStorage.Winner
local Players = game:GetService("Players")
local db = true

npc.Chicken.MeshPart.Touched:Connect(function(part)
	if db == true then
		db = false
		local h = part.Parent:findFirstChild("Humanoid")
		if (h~=nil) then
			player = game.Players:FindFirstChild(h.Parent.Name)
			if (player~=nil) then
				local stats = player:findFirstChild("leaderstats")
				if (stats~=nil) then
					local Score = stats:findFirstChild("Wins")
					if (Score~=nil) then
						Score.Value += 1
					end
				end
			end
		end
		winner.Value = tostring(player)
		
		-- Fetch the thumbnail
		local userId = player.UserId
		local thumbType = Enum.ThumbnailType.HeadShot
		local thumbSize = Enum.ThumbnailSize.Size420x420
		local content, isReady = Players:GetUserThumbnailAsync(userId, thumbType, thumbSize)
		game.Workspace.WinnerSign.Sign.SurfaceGui.PlayerPicture.Image = content
		
		game.ReplicatedStorage.ChickenStatus = false
		npc.Humanoid.Health = 0
		task.wait(1)
		npc:Destroy()
	end
end)
1 Like

Hi! I am still looking into this code, but one thing I did notice is that you’re getting the player from the character’s name. Instead, use GetPlayerFromCharacter. Example:

player = players:GetPlayerFromCharacter(h.Parent)

Try this to start! This is a much better way of getting the player. This may or may not fix your problem, but this is much better practice.

You also don’t need the brackets for if (h~=nil) then ; just do if h ~= nil then !

Edit: You could also just do if h then

I think this is some syntax error. Maybe it gets touched by something that isn’t a player.

local npc = script.Parent
local winner = game.ReplicatedStorage.Winner
local Players = game:GetService("Players")
local db = true

npc.Chicken.MeshPart.Touched:Connect(function(part)
	if db == true then
		db = false
		local h = part.Parent:findFirstChild("Humanoid")
		if (h~=nil) then
    
			local player = game.Players:FindFirstChild(h.Parent.Name)
			if (player~=nil) then
-- proves that the player exists
				local stats = player:findFirstChild("leaderstats")
        local userId = player.UserId
		local thumbType = Enum.ThumbnailType.HeadShot
		local thumbSize = Enum.ThumbnailSize.Size420x420
		local content, isReady = Players:GetUserThumbnailAsync(userId, thumbType, thumbSize)
		game.Workspace.WinnerSign.Sign.SurfaceGui.PlayerPicture.Image = content
		
		game.ReplicatedStorage.ChickenStatus = false
		npc.Humanoid.Health = 0
		task.wait(1)
		npc:Destroy()

				if (stats~=nil) then
					local Score = stats:findFirstChild("Wins")
					if (Score~=nil) then
						Score.Value += 1
					end
				end
			end
		end
		winner.Value = tostring(player)
--[[            Got touched but the Player doesn't exist at the moment

		local userId = player.UserId
		local thumbType = Enum.ThumbnailType.HeadShot
		local thumbSize = Enum.ThumbnailSize.Size420x420
		local content, isReady = Players:GetUserThumbnailAsync(userId, thumbType, thumbSize)
		game.Workspace.WinnerSign.Sign.SurfaceGui.PlayerPicture.Image = content
		
		game.ReplicatedStorage.ChickenStatus = false
		npc.Humanoid.Health = 0
		task.wait(1)
		npc:Destroy()
]]
	end
end)

Found your problem! You’re doing:

local h = part.Parent:findFirstChild("Humanoid")

You made a typo! It should be:

local h = part.Parent:FindFirstChild("Humanoid")
2 Likes

“findFirstChild” is actually the legacy version of “FindFirstChild”. It still works too, even considering its deprecated status.

https://developer.roblox.com/en-us/api-reference/function/Instance/findFirstChild

local players = game:GetService("Players")
local replicated = game:GetService("ReplicatedStorage")
local winner = replicated.Winner
local status = replicated.ChickenStatus

local npc = script.Parent
local chicken = npc.Chicken
local root = chicken.MeshPart

local winnerSign = workspace.WinnerSign
local sign = winnerSign.Sign
local gui = sign.SurfaceGui
local image = gui.PlayerPicture

local debounce = false

root.Touched:Connect(function(part)
	if debounce then return end
	local model = part:FindFirstAncestorOfClass("Model")
	if model then
		local player = players:GetPlayerFromCharacter(model)
		if player then
			debounce = true
			player.leaderstats.Wins.Value += 1		
			winner.Value = player.Name

			local content, isReady = players:GetUserThumbnailAsync(player.UserId, Enum.ThumbnailType.HeadShot, Enum.ThumbnailSize.Size420x420)
			if not isReady then
				task.wait(5)
			end
			
			image.Image = content
			status.Value = false
			task.wait(3)
			npc:Destroy()
			debounce = false
		end
	end
end)