How do I make an accessory worn using AddAccessory stay after death?

I have a button set up so whenever you press it, it fires a remote event to add the accessory in question. How do I make it stay if a player dies?

1 Like

Use a function to check if the Humanoid as died, once they did, add the hat back onto the character.

1 Like

Do you have an example I could go off of?

1 Like

you’ll have to add the accessory every time the player respawns.

what I’d do is have a value that tells the game what the player’s accessory is, and when the player’s character is added, the accessory gets added to the character, something like:

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function()
		local theAccessory = player:WaitForChild("theAccessoryPlayerIsWearing")
		if theAccessory.Value ~= "" then
			--add the accessory
		end
	end)
end)
1 Like

– Server Script

local Players = game:GetService('Players')

Players.PlayerAdded:Connect(function(Player)
	Player.CharacterAdded:Connect(function(Character)
		Character:WaitForChild("Humanoid").Died:Connect(function()
			-- Add the accessory on.
		end)
	end)
end)
1 Like

So if i put this in the script that only activates from a remote event would it still give the accessory whenever a player simply joins?

1 Like

The playeradded function is only to get the player and character, but if you using a remote event (I will write some code).

1 Like

Yeah, you don’t want every player accessing these hats, only the one who previously equipped them.

1 Like

How would I add a value to the player?

1 Like

this should work if you follow the instructions:

game.ReplicatedStorage.ExampleRemote.OnServerEvent:Connect(function(player, accessory) --change "ExampleRemote" to your remote name, and "game.ReplicatedStorage" to wherever its located
	local theAccessory = player:WaitForChild("accessoryEquipped",1)
	if theAccessory then
		
		--put your add accessory code here
		
		theAccessory.Value = accessory --make sure "accessory" is a string; when you fire the remote, make sure you pass the accessory's NAME as the argument
	end
end)

game.Players.PlayerAdded:Connect(function(player)
	local theAccessory = Instance.new("StringValue", player) --adds the value
	theAccessory.Name = "accessoryEquipped"
	
	player.CharacterAdded:Connect(function()
		if theAccessory.Value ~= "" then
			--add the accessory
		end
	end)
end)
1 Like

– Server

local Remote = game.ReplicatedStorage:WaitForChild("remote")

Remote.OnServerEvent:Connect(function(Player)
	print("Died")
	-- Add back the hat
end)

– Client

local Remote = game.ReplicatedStorage:WaitForChild("remote")

local Player = game.Players.LocalPlayer

local Hatequipped = true

local function Died()
	if Hatequipped then 
		Remote:FireServer(Player)
	end
end

Player.Character.Humanoid.Died:Connect(Died)

Of course the boolean would not be true right away since the hat never was equipped. However, you get the point.

2 Likes

Where should I put the client script?

I put it in StarterCharacterScripts.

1 Like