Death Respawn System

essentially with this, I was hoping to make a respawn function where if the character is ready and they die in battle they get respawned with the same character they chose previously. It works so far but why isn’t it giving it the clothes. It just gives the tool and clears characters appearance

local function bulid (player, char, value)
	player:ClearCharacterAppearance()

	if char.Head:FindFirstChild("face") then
		char.Head.face:Destroy()
	end
	wait(0.3)

	local charactertoswapfolder = game.ReplicatedStorage.Characters:FindFirstChild(value)
	if charactertoswapfolder then

		for i, swapclothing in pairs(charactertoswapfolder:GetChildren()) do
			if swapclothing:IsA("Shirt") or swapclothing:IsA("Pants") or swapclothing:IsA("Accessory") or swapclothing:IsA("Hat") then
				swapclothing:Clone().Parent = char
			end
		end

		for i, tools in pairs(charactertoswapfolder:GetChildren()) do
			if tools:IsA("Tool") then
				tools:Clone().Parent = player.Backpack
			end
		end


	end

end


local function spawnin (player)
	local spawns = game.Workspace.Spawns:GetChildren()
	if game.ServerStorage.SERVER.Value == "GAME" then 
		local randomSpawn = spawns[math.random(1, #spawns)]
		if player.PSD.ready.Value == true then 
			player.Character.HumanoidRootPart.CFrame = CFrame.new(randomSpawn.Position)
			player.PSD.status.Value = "ingame"
		end

	end
end


game:GetService('Players').PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		character:WaitForChild("Humanoid").Died:Connect(function()
			wait(6)
			if 	player.PSD.ready.Value == true and game.ServerStorage.SERVER.Value == "GAME" then  
				bulid(player, character, player.PSD.chosen.Value)
				spawnin(player)
				end
			
		end)
	end)
end)
1 Like

Is it possible you could provide an image of the “charactertoswapfolder” contents?

If the tools are being given, it means that this section of the code is the issue:

It’s possible that there may be something wrong with the setup of the folder. If not, then I’m unsure what could be the issue unless ClearCharacterAppearance is somehow being called after the new clothes are applied.

1 Like

Here is the folder
folder

local charactertoswapfolder = game.ReplicatedStorage.Characters:FindFirstChild(value)

Value is the character they have selected. So this line finding the folder assigned to the name of the character

2 Likes

There’s nothing wrong with this setup, so that doesn’t seem to be the problem. Have you tested applying the clothes and accessories to yourself directly? There may be some sort of asset errors.

If this isn’t the case, it may have to do with the ‘char’ variable. Is the face successfully destroyed?

1 Like

It seems like this is the problem the face isn’t getting destroyed. Im not sure why though.

I think I see the issue. bulid() is sending the character who died as an argument instead of the new character being loaded. The other parts of the function use player as a reference, not char. To fix this, you can actually just remove the ‘char’ variable from the arguments and redefine it within the bulid function as this:

local character = player.Character or player.CharacterAdded:Wait() 

This will either find the character right away or it will wait for the character to load in. It should fix your script because it will find the correct character to modify.

On an unrelated note, I advise against using ‘char’ as a variable. While it didn’t break your script, char is a built-in string function (string.char) and it’s not really the most efficient to use function names as variable names. Nothing you necessarily need to worry about, but just some friendly advice.

2 Likes

This worked, and thanks for the extra advice!

2 Likes