Trying to create a script that will apply a humanoid description based off who killed you

I’m not sure how else to describe this. I’m trying to make a script that when a player gets killed, it will load a humanoid description onto a blank rig of the person who killed them. Here’s the script I have:

local rig = script.Parent

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		character.Humanoid.Died:Connect(function()
			local tag = character.Humanoid:FindFirstChild("creator")
			if tag ~= nil then
				local killer = tag.Value
				local id = game.Players:GetUserIdFromNameAsync(killer)
				local newHumanoidDescription = game.Players:GetHumanoidDescriptionFromUserId(id)

				rig.Humanoid:ApplyDescription(newHumanoidDescription)
				print("Description should have been applied")
			end
		end)
	end)
end)

Could someone help me figure out what is wrong? Thank you!

Was the value of a tag the player object or their name?
It needs to be the player’s name.

There are two situations that I think could be the problems for your code.

1. Not Loading Connections whenever Player or Character is loaded

So turns out that there may be a chance that your player and character may have loaded before your script runs, meaning PlayerAdded and Character added won’t fire.

2. You’re assuming the return values exist.

I’m talking about GetUserIdFromNameAsync and GetHumanoidDescriptionFromId.

GetUserIdFromNameAsync makes a web call, meaning it has a chance to fail! You should always wrap async methods in pcall (i took the the code to handle errors from the wiki).

Also, check if the humanoid description exists. It could have not been able to fetch it.

Final Code:

local Players = game:GetService("Players")

local rig = script.Parent

local cache = {}
local function getUserIdFromUsername(name)
	-- First, check if the cache contains the name
	if cache[name] then return cache[name] end
	-- Second, check if the user is already connected to the server
	local player = Players:FindFirstChild(name)
	if player then
		cache[name] = player.UserId
		return player.UserId
	end 
	-- If all else fails, send a request
	local id
	pcall(function ()
		id = Players:GetUserIdFromNameAsync(name)
	end)
	cache[name] = id
	return id
end

local function onPlayerAdded(player)
   local function onCharacterAdded(character)
     character.Humanoid.Died:Connect(function()
        local tag = character.Humanoid:FindFirstChild("creator")
        if tag then
		  local killer = tag.Value
		  local id = getUserIdFromNameAsync(killer)
          if not id then
             return
          end

		  local newHumanoidDescription = Players:GetHumanoidDescriptionFromUserId(id)
          if not newHumanoidDescription then
              return
          end

		  rig.Humanoid:ApplyDescription(newHumanoidDescription)
		  print("Description should have been applied")
	    end
    end)
  end
   
   player.CharacterAdded:Connect(onCharacterAdded)

   if player.Character then
       coroutine.wrap(onCharacterAdded)(player.Character)
   end
end

Players.PlayerAdded:Connect(onPlayerAdded)

for _, player in ipairs(Players:GetPlayers()) do
    coroutine.wrap(onPlayerAdded)(player)
end

(Sorry for bad formatting lol)