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 L=game:GetService("\068\101\098\114\105\115")local C=false local u=0 function ragdoll(M,a)if M==true then if C then spawn(function()if a then Functions_RemoteVelocity:InvokeClient(Player,a)end end)return end C=true u=time()+Settings.UnRagdollCooldown Variables_Ragdoll.Value=true Rig.ToggleRagdollRig(true)Functions_RemoteRagdoll:InvokeClient(Player,true,a)elseif M=="\100\101\097\100"then if Settings.CloneCharacterOnDeath then Player.CharacterRemoving:Connect(function()Character.Archivable=true Humanoid.AutomaticScalingEnabled=false local C=Character:Clone()C.Name=Player.Name.."\'\115 \082\097\103\100\111\108\108"for L,C in pairs(C:GetDescendants())do if C:IsA("\066\097\115\101\083\099\114\105\112\116")or C:IsA("\083\111\117\110\100")then C:Destroy()end end Character:Destroy()C.Parent=workspace Addons.KillHumanoid(C.Humanoid)if Settings.AutoCleanup and Settings.AutoCleanupTime then L:AddItem(C,Settings.AutoCleanupTime)end pcall(function()for L,C in pairs(C:GetDescendants())do if C:IsA("\066\097\115\101\080\097\114\116")then C:SetNetworkOwnershipAuto()end end end)end)end else if not C then return end C=false Variables_Ragdoll.Value=false Rig.ToggleRagdollRig(false)Functions_RemoteRagdoll:InvokeClient(Player,false)end end local M M=(Humanoid:GetPropertyChangedSignal("\072\101\097\108\116\104")):Connect(function()if Humanoid.Health<=0 then M: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)


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)
