Why is this not working? (Character.Humanoid.Died:Connect(function())

Hi I have a character shop script that players buy characters from.

My issue is that when a player dies or resets, they will get their normal character not the equipped one that they bought from the shop.

part of the script:

local DataStoreService = game:GetService("DataStoreService")

local CharacterDataStore = DataStoreService:GetDataStore("CharacterData")

local chars = game.ReplicatedStorage:WaitForChild("Characters")

local classNames = {"Accessory", "Shirt", "Pants", "ShirtGraphic", "BodyColors"}


local function test(player,character)
	for i, descendant in pairs(player.Character:GetDescendants()) do

		if table.find(classNames, descendant.ClassName) or descendant:IsA("Decal") and descendant.Parent.Name == "Head" then

			descendant:Destroy()
		end
	end


	for i, descendant in pairs(chars[character]:GetDescendants()) do


		if table.find(classNames, descendant.ClassName) then

			descendant:Clone().Parent = player.Character

		elseif descendant:IsA("Decal") and descendant.Parent.Name == "Head" then
			descendant:Clone().Parent = player.Character.Head
		end
	end
end

game.Players.PlayerAdded:Connect(function(player)
	
	local chars = Instance.new("Folder", player)
	chars.Name = "CharacterInventory"
	
	local EquippedCharacter = Instance.new("StringValue")
	EquippedCharacter.Name = "EquippedCharacter"
	EquippedCharacter.Parent = player
	
	local data = CharacterDataStore:GetAsync(player.UserId.."-Characters")
	
	if data then
		for i, CharName in pairs(data) do
			if game.ReplicatedStorage:WaitForChild("Characters"):FindFirstChild(CharName) then
				local Val = Instance.new("IntValue")
				Val.Name = CharName
				Val.Parent = chars
			end
		end
	else
		print("No Data")
	end
	
	local equippedData = CharacterDataStore:GetAsync(player.UserId.."-EquippedCharacter")
	
	if equippedData ~= nil then
		EquippedCharacter.Value = equippedData
	else
		print("nil")
	end
	
	player.CharacterAdded:Connect(function(character)
		character.Humanoid.Died:Connect(function()
			test(player,EquippedCharacter.Value)
		end)
	end)
end)

When a player buys and equips from the shop it works perfectly, until the player dies or resets.

What did I do to try and solve?
I added these lines here, but it didn’t work.

player.CharacterAdded:Connect(function(character)
	character.Humanoid.Died:Connect(function()
		test(player,EquippedCharacter.Value) -- ignore my function being called test
	end)
end)

I think you have to wait for it to spawn a new character. You can’t set the values when the character is already dead, you have to wait for it to respawn first. You can add a

player.CharacterAdded:Wait()

before

test(player, EquippedCharacter.Value)

It didn’t work

player.CharacterAdded:Connect(function(character)
	player.character.Humanoid.Died:Connect(function()
		player.CharacterAdded:Wait()
		test(player,EquippedCharacter.Value)
	end)
end)
1 Like

take off player.character.Humanoid.Died, it should be just

character:WaitForChild("Humanoid").Died:Connect(function()
end)
1 Like

It didn’t work

	player.CharacterAdded:Connect(function(character)
		character:WaitForChild("Humanoid").Died:Connect(function()
			player.CharacterAdded:Wait()
			test(player,EquippedCharacter.Value)
		end)
	end)

Try yielding the part of the test method that gives the objects for a little while until the player’s character is renewed(respawned), so the script doesn’t give it to the wrong character (The old character will be destoryed)

Try without the humanoid died event.

player.CharacterAdded:Connect(function(character)
	test(player,EquippedCharacter.Value)
end)

how will I do this exactly???

Something like this:

local PlayerService = game:GetService("Players")

local function test(player,character)
	for i, descendant in pairs(player.Character:GetDescendants()) do

		if table.find(classNames, descendant.ClassName) or descendant:IsA("Decal") and descendant.Parent.Name == "Head" then

			descendant:Destroy()
		end
	end

    task.wait(PlayerService.RespawnTime/2) -- Yields half of the players's respawn time

    local Character = player.Character or player.CharacterAdded:Wait() -- [[Pre Defined Character Variable]] Yields untill character has loaded 

	for i, descendant in pairs(chars[character]:GetDescendants()) do


		if table.find(classNames, descendant.ClassName) then

			descendant:Clone().Parent = Character

		elseif descendant:IsA("Decal") and descendant.Parent.Name == "Head" then
			descendant:Clone().Parent = Character.Head
		end
	end
end