My ragdoll script doesn't keep the players hair and accessories

So, I’ve used this code for a while and to not leave the hair flying around I’ve just destroyed it. But It would be nice to keep the hair on the body.

local Players = game:GetService("Players")

Players.PlayerAdded:Connect(function(Player)
	Player.CharacterAdded:Connect(function(Character)
		Character.Humanoid.BreakJointsOnDeath = false
		
		while true do
			
			if Character.Humanoid.Health == 0 then
				
				for _, v in pairs(Character:GetDescendants()) do
					
					if v:IsA("Accessory") then
						v:Destroy()
					end
					
					if v:IsA("Motor6D") then
						local Attachment0, Attachment1 = Instance.new("Attachment"), Instance.new("Attachment")
						Attachment0.CFrame = v.C0
						Attachment1.CFrame = v.C1
						Attachment0.Parent = v.Part0
						Attachment1.Parent = v.Part1

						local BallSocketConstraint = Instance.new("BallSocketConstraint")
						BallSocketConstraint.Attachment0 = Attachment0
						BallSocketConstraint.Attachment1 = Attachment1
						BallSocketConstraint.Parent = v.Parent

						v:Destroy()
					end
					
				end
				Character.Humanoid.BreakJointsOnDeath = true
				
				Character.HumanoidRootPart.CanCollide = false
				Character.Head.CanCollide = true

				
			end
			wait(0.3)
		end
		
	end)
end)
1 Like

I’ve just quickly updated my code to remove the while true loop:

local Players = game:GetService("Players")

Players.PlayerAdded:Connect(function(Player)
	Player.CharacterAdded:Connect(function(Character)
		Character.Humanoid.BreakJointsOnDeath = false
		
		Character.Humanoid.Died:Connect(function()
			for _, v in pairs(Character:GetDescendants()) do

				if v:IsA("Accessory") then
					v:Destroy()
				end

				if v:IsA("Motor6D") then
					local Attachment0, Attachment1 = Instance.new("Attachment"), Instance.new("Attachment")
					Attachment0.CFrame = v.C0
					Attachment1.CFrame = v.C1
					Attachment0.Parent = v.Part0
					Attachment1.Parent = v.Part1

					local BallSocketConstraint = Instance.new("BallSocketConstraint")
					BallSocketConstraint.Attachment0 = Attachment0
					BallSocketConstraint.Attachment1 = Attachment1
					BallSocketConstraint.Parent = v.Parent

					v:Destroy()
				end

			end
			Character.Humanoid.BreakJointsOnDeath = true

			Character.HumanoidRootPart.CanCollide = false
			Character.Head.CanCollide = true

			wait(0.3)
		end)

	end)
end)

Problem was that you set the property BreakJointsOnDeath to true which will delete accessory welds once the character dies.

local Players = game:GetService("Players")

Players.PlayerAdded:Connect(function(Player)
	Player.CharacterAdded:Connect(function(Character)
		Character.Humanoid.BreakJointsOnDeath = false

		Character.Humanoid.Died:Connect(function()
			for _, v in pairs(Character:GetDescendants()) do


				if v:IsA("Motor6D") then
					local Attachment0, Attachment1 = Instance.new("Attachment"), Instance.new("Attachment")
					Attachment0.CFrame = v.C0
					Attachment1.CFrame = v.C1
					Attachment0.Parent = v.Part0
					Attachment1.Parent = v.Part1

					local BallSocketConstraint = Instance.new("BallSocketConstraint")
					BallSocketConstraint.Attachment0 = Attachment0
					BallSocketConstraint.Attachment1 = Attachment1
					BallSocketConstraint.Parent = v.Parent

					v:Destroy()
				end

			end

			Character.HumanoidRootPart.CanCollide = false
			Character.Head.CanCollide = true

			wait(0.3)
		end)

	end)
end)

This should keep the player’s accessories when they die.

1 Like

Wow, thanks it works. I would’ve thought I needed to add another attachment or weld.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.