function safeClone(B)local f=B.Archivable B.Archivable=false local e=B:Clone()B.Archivable=f return e end function characterAdded(B,f)B.CharacterAppearanceLoaded:wait()wait(.1)local e=f:FindFirstChildOfClass("\072\117\109\097\110\111\105\100")buildRagdoll(e)end function characterRemoving(B)local f=B:FindFirstChildOfClass("\072\117\109\097\110\111\105\100")if f:GetState()~=Enum.HumanoidStateType.Dead then return end local e=safeClone(B)local L=e:FindFirstChildOfClass("\072\117\109\097\110\111\105\100")L.DisplayDistanceType=Enum.HumanoidDisplayDistanceType.None L.AutomaticScalingEnabled=false local N=B:FindFirstChild("\065\110\105\109\097\116\101")local m=B:FindFirstChild("\083\111\117\110\100")local z=B:FindFirstChild("\072\101\097\108\116\104")if N then N:Destroy()end if m then m:Destroy()end if z then z:Destroy()end e.Parent=workspace L:ChangeState(Enum.HumanoidStateType.Dead)end function playerAdded(B)B.CharacterAdded:connect(function(f)characterAdded(B,f)end)B.CharacterRemoving:Connect(characterRemoving)if B.Character then characterAdded(B,B.Character)end end Players.PlayerAdded:connect(playerAdded)

function safeClone(instance)
	local oldArchivable = instance.Archivable
	
	instance.Archivable = false
	local clone = instance:Clone()
	instance.Archivable = oldArchivable
	
	return clone
end

function characterAdded(player, character)
	player.CharacterAppearanceLoaded:wait()
	wait(0.1)
	
	local humanoid = character:FindFirstChildOfClass("Humanoid")
	buildRagdoll(humanoid)
end

function characterRemoving(character)
	local humanoid = character:FindFirstChildOfClass("Humanoid")
	if humanoid:GetState() ~= Enum.HumanoidStateType.Dead then
		return
	end

	local clone = safeClone(character)
	local cloneHumanoid = clone:FindFirstChildOfClass("Humanoid")

	-- Don't clutter the game with nameplates / healthbars
	cloneHumanoid.DisplayDistanceType = Enum.HumanoidDisplayDistanceType.None
	-- Roblox will try to rebuild joints when the clone is parented to Workspace and
	-- break the ragdoll, so disable automatic scaling to prevent that. We don't need
	-- it anyway since the character is already scaled from when it was originally created
	cloneHumanoid.AutomaticScalingEnabled = false
	
	-- Clean up junk so we have less scripts running and don't have ragdolls
	-- spamming random sounds
	local animate = character:FindFirstChild("Animate")
	local sound = character:FindFirstChild("Sound")
	local health = character:FindFirstChild("Health")
	
	if animate then
		animate:Destroy()
	end
	if sound then
		sound:Destroy()
	end
	if health then
		health:Destroy()
	end
	
	clone.Parent = workspace
	
	-- State is not preserved when cloning. We need to set it back to Dead or the
	-- character won't ragdoll. This has to be done AFTER parenting the character
	-- to Workspace or the state change won't replicate to clients that can then
	-- start simulating the character if they get close enough
	cloneHumanoid:ChangeState(Enum.HumanoidStateType.Dead)
end

function playerAdded(player)
	player.CharacterAdded:connect(function(character)
		characterAdded(player, character)
	end)
	
	player.CharacterRemoving:Connect(characterRemoving)
	
	if player.Character then
		characterAdded(player, player.Character)
	end
end

Players.PlayerAdded:connect(playerAdded)