Rig.Character=Character Rig.CreateFolders()Rig.FindParts()Rig.CreateAltRootMotor()Rig.ScaleAltRootMotorOffset()Rig.CreateConstraints()Rig.BuildConstraintRig()if Settings.CreateAccessoryConstraints==true then Rig.CreateAccessoryConstraints()end Rig.CreateNoClipCollisionGroup()Rig.TakeCareOfRootPartAndHead()Rig.CreateCustomHeadCollisionBox()Rig.CreateNoCollisionConstraints()local G=game:GetService("\068\101\098\114\105\115")local N=false local t=0 function ragdoll(c,j)if c==true then if N then spawn(function()if j then Functions_RemoteVelocity:InvokeClient(Player,j)end end)return end N=true t=time()+Settings.UnRagdollCooldown Variables_Ragdoll.Value=true Rig.ToggleRagdollRig(true)Functions_RemoteRagdoll:InvokeClient(Player,true,j)elseif c=="\100\101\097\100"then if Settings.CloneCharacterOnDeath then Player.CharacterRemoving:Connect(function()Character.Archivable=true Humanoid.AutomaticScalingEnabled=false local N=Character:Clone()N.Name=Player.Name.."\'\115 \082\097\103\100\111\108\108"for G,N in pairs(N:GetDescendants())do if N:IsA("\066\097\115\101\083\099\114\105\112\116")or N:IsA("\083\111\117\110\100")then N:Destroy()end end Character:Destroy()N.Parent=workspace Addons.KillHumanoid(N.Humanoid)if Settings.AutoCleanup and Settings.AutoCleanupTime then G:AddItem(N,Settings.AutoCleanupTime)end pcall(function()for G,N in pairs(N:GetDescendants())do if N:IsA("\066\097\115\101\080\097\114\116")then N:SetNetworkOwnershipAuto()end end end)end)end else if not N then return end N=false Variables_Ragdoll.Value=false Rig.ToggleRagdollRig(false)Functions_RemoteRagdoll:InvokeClient(Player,false)end end local c c=(Humanoid:GetPropertyChangedSignal("\072\101\097\108\116\104")):Connect(function()if Humanoid.Health<=0 then c:Disconnect()pcall(function()Character.Health:Destroy()end)Addons.KillHumanoid(Humanoid)ragdoll(true)if Settings.BreakJointsOnDeath then Addons.DestroyMotors(Rig.Motors)end ragdoll("\100\101\097\100")end end);(Variables_Ragdoll:GetPropertyChangedSignal("\086\097\108\117\101")):Connect(function()if Humanoid.Health>0 then ragdoll(Variables_Ragdoll.Value)end end)
--Humanoid:SetStateEnabled(Enum.HumanoidStateType.Dead, false)

Rig.Character = Character
Rig.CreateFolders()
Rig.FindParts()
Rig.CreateAltRootMotor()
Rig.ScaleAltRootMotorOffset()
Rig.CreateConstraints()
Rig.BuildConstraintRig()
if Settings.CreateAccessoryConstraints == true then
	Rig.CreateAccessoryConstraints()
end
Rig.CreateNoClipCollisionGroup()
Rig.TakeCareOfRootPartAndHead()
Rig.CreateCustomHeadCollisionBox()
Rig.CreateNoCollisionConstraints()

local Debris = game:GetService("Debris")
local IsRagdollNow = false

local CurrentCooldown = 0

function ragdoll(Mode, Velocity)
	if Mode == true then
		if IsRagdollNow then
			spawn(function()
				if Velocity then
					Functions_RemoteVelocity:InvokeClient(Player, Velocity)
				end
			end)
			return
		end
		IsRagdollNow = true
		CurrentCooldown = time() + Settings.UnRagdollCooldown
		Variables_Ragdoll.Value = true
		Rig.ToggleRagdollRig(true)
		Functions_RemoteRagdoll:InvokeClient(Player, true, Velocity)
	elseif Mode == "dead" then
		if Settings.CloneCharacterOnDeath then
			Player.CharacterRemoving:Connect(function()
				Character.Archivable = true
				Humanoid.AutomaticScalingEnabled = false
				local CharClone = Character:Clone()
				CharClone.Name = Player.Name .. "'s Ragdoll"
				for _, v in pairs(CharClone:GetDescendants()) do
					if v:IsA("BaseScript") or v:IsA("Sound") then
						v:Destroy()
					end
				end
				Character:Destroy()
				CharClone.Parent = workspace
				Addons.KillHumanoid(CharClone.Humanoid)
				if Settings.AutoCleanup and Settings.AutoCleanupTime then
					Debris:AddItem(CharClone, Settings.AutoCleanupTime)
				end
				pcall(function()
					for _, v in pairs(CharClone:GetDescendants()) do
						if v:IsA("BasePart") then
							v:SetNetworkOwnershipAuto()
						end
					end
				end)
			end)
		end
	else
		if not IsRagdollNow then
			return
		end
		IsRagdollNow = false
		Variables_Ragdoll.Value = false
		Rig.ToggleRagdollRig(false)
		Functions_RemoteRagdoll:InvokeClient(Player, false)
	end
end

local HealthConnection
HealthConnection = Humanoid:GetPropertyChangedSignal("Health"):Connect(function()
	if Humanoid.Health <= 0 then
		HealthConnection:Disconnect() --Preparing for death sequence
		pcall(function()
			Character.Health:Destroy()
		end)

		-- PLACE FOR CODE MEANT TO HAPPEN BEFORE ACTUALLY KILLING					--Executing code that needs to run before death to avoid bugs

		--Humanoid:SetStateEnabled(Enum.HumanoidStateType.Dead, true)				--Killing
		Addons.KillHumanoid(Humanoid)

		ragdoll(true) --Ragdolling

		if Settings.BreakJointsOnDeath then
			Addons.DestroyMotors(Rig.Motors)
		end --Breaking joints after ragdolling to ensure consistency with ragdolling normally

		ragdoll("dead") --Executing post-mortem events
	end
end)

Variables_Ragdoll:GetPropertyChangedSignal("Value"):Connect(function()
	if Humanoid.Health > 0 then
		ragdoll(Variables_Ragdoll.Value)
	end
end)
