Keeping accesoires on death

Hello so i don’t understand why this code isn’t working:

function Infinity()
	player.CharacterAdded:Connect(function(char)
		if char:FindFirstChild("BackPack2") then
			game.ServerStorage.Armor.BackPack2:Clone().Parent = char
		end
		if char:FindFirstChild("BackPack") then
			game.ServerStorage.Armor.BackPack:Clone().Parent = char
		end
		if char:FindFirstChild("Vest") then
			game.ServerStorage.Armor.Vest:Clone().Parent = char
		end
		if char:FindFirstChild("Vest2") then
			game.ServerStorage.Armor.Vest2:Clone().Parent = char
		end
		if char:FindFirstChild("Vest3") then
			game.ServerStorage.Armor.Vest3:Clone().Parent = char
		end
	end)
end
hum.Died:Connect(Infinity)

I think the issue may be that the character doesn’t spawn with those accessories bu default, so the checks you’re doing aren’t returning what you want them to. Try changing if char:FindFirstChild to if not char:FindFirstChild.

1 Like

Ahh, I see, I misunderstood. It seems like you’ll have to add a value or something to the player instance to signify that they’re supposed to have them equipped instead of doing that check on the character, because it would be the default every time they spawn. So, instead of doing that char:FindFirstChild check, you should assign a value or add them to a table to identify that this player should be given accessories when they spawn.

You might wanna try using Attributes on the player.

function Infinity()
player.CharacterAdded:connect(function(char)
  if player:GetAttribute("KeepAccessories") then
      game.ServerStorage.Armor.BackPack2:Clone().Parent = char
      game.ServerStorage.Armor.BackPack:Clone().Parent = char
      game.ServerStorage.Armor.Vest:Clone().Parent = char
      game.ServerStorage.Armor.Vest2:Clone().Parent = char
     game.ServerStorage.Armor.Vest3:Clone().Parent = char
   end
end)
hum.Died:Connect(Infinity)
1 Like

Now this is my script:

function Infinity()
	player.CharacterAdded:Connect(function()
		for _,v in pairs(game.ServerStorage.Armor:GetChildren()) do
			if v:GetAttribute("Keep") then
				if char:FindFirstChild("BackPack2") then
					game.ServerStorage.Armor.BackPack2:Clone().Parent = char
				end
				if char:FindFirstChild("BackPack") then
					game.ServerStorage.Armor.BackPack:Clone().Parent = char
				end
				if char:FindFirstChild("Vest") then
					game.ServerStorage.Armor.Vest:Clone().Parent = char
				end
				if char:FindFirstChild("Vest2") then
					game.ServerStorage.Armor.Vest2:Clone().Parent = char
				end
				if char:FindFirstChild("Vest3") then
					game.ServerStorage.Armor.Vest3:Clone().Parent = char
				end
			end
		end
	end)
end
hum.Died:Connect(Infinity)

but it says:

trying to set locked parent!

I think i may have complicated you a little with attributes. The attribute must be added to the Player. Here is an example:

game.Players[player]:SetAttribute("Keep",true)

You can replace your script with this one and check if things work:

function Infinity()
	player.CharacterAdded:Connect(function(char)
             if player:GetAttribute("Keep") then
	        	for _,v in pairs(game.ServerStorage.Armor:GetChildren()) do
				v:Clone().Parent = char
			end
		end
	end)
end
hum.Died:Connect(Infinity)

Also may i know why you are checking to see if the player’s character has the armor descendants?

Because the armor is an accesoire with needs to be in the character

You are trying to see if the player’s character had the armor accessories before he died?

I am trying to check if the player has the accesoire after he died!

Oh then i understand what you are trying to do.
Replace your script with this one:

function Infinity()
	player.CharacterAdded:Connect(function(char)
      repeat wait() until char:FindFirstChild("UpperTorso") or char:FindFirstChild("Torso")
		if not char:FindFirstChild("BackPack2") then
			game.ServerStorage.Armor.BackPack2:Clone().Parent = char
		end
		if not char:FindFirstChild("BackPack") then
			game.ServerStorage.Armor.BackPack:Clone().Parent = char
		end
		if not char:FindFirstChild("Vest") then
			game.ServerStorage.Armor.Vest:Clone().Parent = char
		end
		if not char:FindFirstChild("Vest2") then
			game.ServerStorage.Armor.Vest2:Clone().Parent = char
		end
		if not char:FindFirstChild("Vest3") then
			game.ServerStorage.Armor.Vest3:Clone().Parent = char
		end
	end)
end
hum.Died:Connect(Infinity)

if not? That would mean that i get the accesoire when i don’t have the accesoire before death :frowning:

player.CharacterAdded gets fired after the player respawns so inside this function you are checking to see if the player has the accessories after he respawns which he will most likely not have them so if not char:FindFirstChild("BackPack2") for example checks if the player has the “BackPack2” inside his character and if he doesn’t have it then it clones it from ServerStorage and parents it to his character.

Sorry if most of the things i said might have complicated you

ok so i tested it but it doesn’t worked, sadly :frowning:

Can you send the whole script in here?

of course i do it is in StarterPlayer-StarterCharacterScripts:

local char = script.Parent
local player = game.Players:GetPlayerFromCharacter(char)
local hum = char:WaitForChild("Humanoid")
function FaceonDeath()
	local head = char:WaitForChild("Head")
	local face = head:WaitForChild("face")

	face:Destroy()
	local newFace = Instance.new("Decal",head)
	newFace.Texture = "rbxassetid://1367341140"
end
function Infinity()
	player.CharacterAdded:Connect(function(char)
		repeat wait() until char:FindFirstChild("UpperTorso") or char:FindFirstChild("Torso")
		if not char:FindFirstChild("BackPack2") then
			game.ServerStorage.Armor.BackPack2:Clone().Parent = char
		end
		if not char:FindFirstChild("BackPack") then
			game.ServerStorage.Armor.BackPack:Clone().Parent = char
		end
		if not char:FindFirstChild("Vest") then
			game.ServerStorage.Armor.Vest:Clone().Parent = char
		end
		if not char:FindFirstChild("Vest2") then
			game.ServerStorage.Armor.Vest2:Clone().Parent = char
		end
		if not char:FindFirstChild("Vest3") then
			game.ServerStorage.Armor.Vest3:Clone().Parent = char
		end
	end)
end
hum.Died:Connect(FaceonDeath,Infinity)

Why do you need to listen 2 connections, which are Died and CharacterAdded? What you only need is the CharacterAdded, while Died has nothing to do with the accessories that you want to keep.

Also, what you are doing here is checking if the character has those accessories, which will return false, and none of those conditions will become true, because the character will always spawn with the default character. Since the player is always respawning with the default character, you don’t need to check if the character has these accessories.

I also recommend that you add attributes with those accessories, which would help lessen your manual labor of typing each accessory one by one, since there are only 5. Instead, store them on an array or table.

local Armor = game:GetService("ServerStorage")

local keep = {
	"BackPack2",
	"BackPack",
	"Vest3",
	"Vest2",
	"Vest",
}

for index = #keep, 1, -1 do
	local accessory = Armor:FindFirstChild(keep[index])

	if accessory then
		keep[index] = accessory
	else
		table.remove(keep, index)
		warn(("%s is not a valid member of %s"):format(name, Armor))
	end
end

game:GetService("Players").PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		for _, accessory in ipairs(keep) do
			accessory:Clone().Parent = character
		end
	end)
end)

I copied the script but it didn’t work it still doesn’t give me the accessory :frowning:

Is there an error? Try print the result of the table keep.

Edit: I edited the code a little bit from the first for loop it’s now:

for index = #keep, 1, -1 do
	local accessory = Armor:FindFirstChild(keep[index])

	if accessory then
		keep[index] = accessory
	else
		table.remove(keep, index)
		warn(("%s is not a valid member of %s"):format(name, Armor))
	end
end

and where is the variable name at warn(name)?

You can just add it simply create a variable “name”.

local name = keep[index]