Accessory isn't saving when the player dies

So, I’ve been scripting my Accessory data store recently and I ran into a problem where, when the player resets, the player loses the item. And I added a PlayerAdded function but it still doesn’t work. Any help?

Here’s the code for the PlayerAdded function:

game.Players.PlayerAdded:Connect(function(player)
	local DominoValue = game.ServerStorage.doesPlayerHaveAccessory.Domino
	local DominoItem = game.ReplicatedStorage.Hats.Domino
	local Character = player.Character or player.CharacterAdded:Wait()

	if DominoValue.Value == true then
		local Folder = Instance.new("Folder")
		Folder.Name = "Hats"
		Folder.Parent = Character.Head
		local Head = Character.Head
		local New_Hat = DominoItem:Clone()
		New_Hat.Parent = Folder
		New_Hat.Position = Head.Position
		local Weld = Instance.new("Weld")
		Weld.Parent = New_Hat
		Weld.Part0,Weld.Part1 = Head, New_Hat
		Weld.C0 = CFrame.new(0,1.4,0)
		Weld.C1 = Head.HatAttachment.CFrame
	end
end)
1 Like

Im not the best scripter but maybe before if DominoValue.Value == true
but a while true do so it keeps checking if you have the value because i think its checking it once then not checking again so if the player resets it doesnt check again
game.Players.PlayerAdded:Connect(function(player)
local DominoValue = game.ServerStorage.doesPlayerHaveAccessory.Domino
local DominoItem = game.ReplicatedStorage.Hats.Domino
local Character = player.Character or player.CharacterAdded:Wait()

while true do wait(1)
if DominoValue.Value == true then
	local Folder = Instance.new("Folder")
	Folder.Name = "Hats"
	Folder.Parent = Character.Head
	local Head = Character.Head
	local New_Hat = DominoItem:Clone()
	New_Hat.Parent = Folder
	New_Hat.Position = Head.Position
	local Weld = Instance.new("Weld")
	Weld.Parent = New_Hat
	Weld.Part0,Weld.Part1 = Head, New_Hat
	Weld.C0 = CFrame.new(0,1.4,0)
		Weld.C1 = Head.HatAttachment.CFrame
		end
end

end)

maybe try this and tell me what happens.

1 Like

So, it didn’t work. And all it did was lag the game cause it kept adding hats into the player’s head. And it didn’t save when the player died.

OOf. I would recommend watching this then https://www.youtube.com/watch?v=8FsAg9ZD_d0

That didn’t help cause that is saving an inventory. I’m saving player accessories.

Then im lost cause i use inventory :sweat_smile:

Oh. Makes sense. I’ll just wait for some other people to answer.

1 Like

you should have actually checked if the player already owns the hat

while true do wait(1)
if DominoValue.Value == true and Character:FindFirstChild("NAMEOFTHEHAT") == nil then
	local Folder = Instance.new("Folder")
	Folder.Name = "Hats"
	Folder.Parent = Character.Head
	local Head = Character.Head
	local New_Hat = DominoItem:Clone()
	New_Hat.Parent = Folder
	New_Hat.Position = Head.Position
	local Weld = Instance.new("Weld")
	Weld.Parent = New_Hat
	Weld.Part0,Weld.Part1 = Head, New_Hat
	Weld.C0 = CFrame.new(0,1.4,0)
		Weld.C1 = Head.HatAttachment.CFrame
		end
end

idk if i put the right amount of ends but here you go

That changed nothing. And when I join, it keeps spawning hat repetitively.

I just had this problem too and you should use CharacterAdded
this will make it so that when the player respawns,CharacterAddded still functions

this is what i did

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		local player = game:GetService("Players"):GetPlayerFromCharacter(character)
	repeat wait() until player.Character
	local character = player.Character
	for i,v in pairs(game.Workspace:WaitForChild(player.Name):GetChildren()) do
		if v:IsA("Accessory") or v:IsA("Hat") or v:IsA("Shirt") or v:IsA("Pants") then
			v:Destroy()
		end
	end	
	
	local headc = character.Head:GetChildren()
	for i,v in pairs(game.Workspace:WaitForChild(player.Name).Head:GetChildren()) do
		if v:IsA("Decal") then 
			v:Destroy()
		end
	end
	
	
	
	local hair = player.leaderstats.Hair.Value
	local hairs = game.ReplicatedStorage.Hairs:GetChildren()
	wait(1)
	for i,v in pairs(hairs) do
		if v:IsA("Accessory") and v.Name == hair then
			local hairc = v:Clone()
			character.Humanoid:AddAccessory(hairc)
		end
	end

	
	local outfit = player.leaderstats.Outfit.Value
	local outfits = game.ReplicatedStorage.Outfits:GetChildren()
	wait(1)
	for i,v in pairs(outfits) do
		if v:IsA("Folder") and v.Name == outfit then
			local newv = v:GetChildren()
			for i,v in pairs(newv) do
				if v:IsA("Shirt") then
					local shirt = v:Clone()
					shirt.Parent = character
			end

				if v:IsA("Pants") then
					local pants = v:Clone()
					pants.Parent = character
		end
					
			
			end
		end
	end
	
	local face = player.leaderstats.Face.Value
	local faces = game.ReplicatedStorage.Faces:GetChildren()
	wait(1)
	for i,v in pairs(faces) do
		if v:IsA("Decal") and v.Name == face then
			local facec = v:Clone()
			facec.Parent = player.Character.Head
		end
	end
	end)
	end)
1 Like

I didn’t test it yet but, the script analysis is giving me an error saying that there is a problem with the CharacterAdded event. And the error is coming from a variable saying, local data

I dont remember using a variable called local data, maybe its because my script is getting values from the player DataStores

I added the local data for the data store.

make sure it’s a server script inside server script service?

Yes. It is a script inside of server script service.

Did you test it yet?
(the script also clears the player’s original character before adding the datastored custom character)

Chhhhhhhhh

I tested it and it didn’t work.

game.Players.PlayerAdded:Connect(function(player)--when the player joins
 player.CharacterAdded:Connect(function(character)--when the player's character is loaded then function
  local player = game:GetService("Players"):GetPlayerFromCharacter(character)-- gets the player if you want to use that as a variable
wait(how long you want to wait)-- to make sure
  --give the person their hats and clothings and stuff
 end)
end)

When the player’s character dies, when it respawns then that still triggers the Character Added function so the script will give the player hats and stuff again

It still doesn’t work and I added an error function so it would let me know if there was an error, and it’s not telling me anything

Wait hold on, the output just said doesPlayerHaveAccessory is not a valid member of ServerStorage “ServerStorage”. And the item doesPlayerHaveAccessory is a folder that has a bool value in it to check if the player has the item.