Clothes apply to character when character is already dead

My issue with the following scripts is that the player receives the saved clothes when they die instead of when they respawned.

script: ServerScriptServices

game.Players.PlayerAdded:Connect(function(plr)
	
	local F = Instance.new("Folder", plr)F.Name = "Stats" 
	local ShirtValue = Instance.new("StringValue", F)ShirtValue.Name = "ShirtID" ShirtValue.Value = "N/A"
	
end)

local SE = game.ReplicatedStorage.ShirtEvent

SE.OnServerEvent:Connect(function(plr)
	local char = plr:WaitForChild("Character")
	if plr.Stats.ShirtID.Value == "SJT" then
		char:WaitForChild("Shirt").ShirtTemplate = script.Shirt1.ShirtTemplate
		print("Shirt applied to "..plr.Name)
	end
end)

--script: StarterCharacterScripts

local plr = game.Players.LocalPlayer

local char = script.Parent

local hum = char:WaitForChild("Humanoid")

hum.Died:Connect(function()

print(plr.Name.. " Died!")

wait(8)

game.ReplicatedStorage.ShirtEvent:FireServer(plr)

end)

--Local script: StarterCharacterScripts

I think an easier thing to do would just be utilizing playeradded and character added in server

game.Players.PlayerAdded:Connect(function(plr)
local F = Instance.new("Folder", plr)F.Name = "Stats" 
	local ShirtValue = Instance.new("StringValue", F)ShirtValue.Name = "ShirtID" ShirtValue.Value = "N/A"
	
	plr.CharacterAdded:Connect(function(char)

--its best to access either through workspace or from plr, dont use char above, may run into issues.
local Character = plr:WaitForChild("Character")
	if plr.Stats.ShirtID.Value == "SJT" then
		Character:WaitForChild("Shirt").ShirtTemplate = script.Shirt1.ShirtTemplate
		print("Shirt applied to "..plr.Name)
	end

	end)
end)

Character still wears the original shirt. Thanks for helping though.

I’m not sure what you mean original shirt, but I assume that the character’s shirt didn’t change? probably because the shirtid value didn’t change or not quick enough.

you can also test making the value “SJT” upon player added and not just “N/A”

You’re probably right, should I do a wait() line?

no you should not use waits in this it is bad practice when you can have a listening function waiting for change to the value

game.Players.PlayerAdded:Connect(function(plr)
local F = Instance.new("Folder", plr)F.Name = "Stats" 
	local ShirtValue = Instance.new("StringValue", F)ShirtValue.Name = "ShirtID" ShirtValue.Value = "N/A"
	local Connection
        
	plr.CharacterAdded:Connect(function(char)

        if Connection then Connection:Disconnect() end

--its best to access either through workspace or from plr, dont use char above, may run into issues.
local Character = plr:WaitForChild("Character")


--if value changed and we can detect here
	if plr.Stats.ShirtID.Value == "SJT" then
		Character:WaitForChild("Shirt").ShirtTemplate = script.Shirt1.ShirtTemplate
		print("Shirt applied to "..plr.Name)
	end
Connection = plr.Stats.ShirtID:GetPropertyChangedSignal("Value"):Connect(function()
--if value changes later on
       if plr.Stats.ShirtID.Value == "SJT" then
		Character:WaitForChild("Shirt").ShirtTemplate = script.Shirt1.ShirtTemplate
		print("Shirt applied to "..plr.Name)
	end
end)

	end)
end)

although admittedly you would want to manually change the shirt in the exact code block where you intend to change the shirtvalue. I’m not sure why you are aiming for a more convoluted option here

I have tested it, it didn’t work, I’ll try from the robloxplayer and not the roblox studio.

It shouldnt be a studio/player issue you should verify that your code does infact change

plr.Stats.ShirtID.Value

Yeah, it doesn’t apply to the character

Try CharacterAppearanceLoaded instead of CharacterAdded.
This event fires when the the full appearance of a Player’s Player.Character has been inserted.
https://developer.roblox.com/en-us/api-reference/event/Player/CharacterAppearanceLoaded

I’ve tried but no print lines are in output. So I guess it didn’t work :confused:

What’s your current code?
Will be easier to debug if you show us.