Ragdoll scripts causing player to die twice

I’m having an issue with these ragdoll scripts.

When the player resets their character, the player’s health is set to 0 twice.

As you can see in this video, the player stops moving and the death sound plays twice. In addition, I had a custom death sound play in sound service when the player dies, and that ran twice as well. (It was disabled during the video.)

The ragdoll runs on three scripts, one module script in ReplicatedFirst, one localScript in StarterCharacterScripts, and one regular script in ServerScriptService.

Here is the code for the module script

local players = game:GetService("Players")
local replicatedstorage = game:GetService("ReplicatedStorage")

local playerEvents = replicatedstorage:WaitForChild("Player")
Ragdoll = {}
function Ragdoll.RagdollCharacter(char : model)
	local player = players:GetPlayerFromCharacter(char)
	if player then
		playerEvents:WaitForChild("Ragdoll"):FireClient(player, true, "Ragdolling")
	end
	local hum = char:FindFirstChild("Humanoid")
	
	if hum.RigType ==Enum.HumanoidRigType.R6 then
		local Head= char:FindFirstChild("Head")
		local Torso= char:FindFirstChild("Torso")
		local HumanoidRootPart= char:FindFirstChild("HumanoidRootPart")
		local RightArm= char:FindFirstChild("Right Arm")
		local LeftArm= char:FindFirstChild("Left Arm")
		local RightLeg= char:FindFirstChild("Right Leg")
		local LeftLeg= char:FindFirstChild("Left Leg")
			--
		if Torso and not Torso:FindFirstChild("NeckJoint") then
			
			--Attaching Neck to Torso
			local neckAttach = Instance.new("Attachment")
			neckAttach.Parent = Head
			neckAttach.Name = "NeckAttachment"
			neckAttach.Position = Vector3.new(0,-0.5,0)
			neckAttach.Orientation = Vector3.new(0,0,0)
			neckAttach:SetAttribute("Ragdoll", true)
			
			local torsoNeckAttach = Instance.new("Attachment")
			torsoNeckAttach.Parent = Torso
			torsoNeckAttach.Name = "TorsoNeckAttachment"
			torsoNeckAttach.Position = Vector3.new(0,1,0)
			torsoNeckAttach.Orientation = Vector3.new(0,0,0)
			torsoNeckAttach:SetAttribute("Ragdoll", true)
			
			local neckJoint = script.Neck:Clone()
			neckJoint.Parent = Torso
			neckJoint.Name = "NeckJoint"
			neckJoint.Attachment0 = neckAttach
			neckJoint.Attachment1 = torsoNeckAttach
			neckAttach:SetAttribute("Ragdoll", true)
			torsoNeckAttach:SetAttribute("Ragdoll", true)
			
			
			--Attaching Right Arm
			
			
			local RArmAttach = Instance.new("Attachment")
			RArmAttach.Parent = RightArm
			RArmAttach.Name = "RightArmAttachment"
			RArmAttach.Position = Vector3.new(-0.5,0.5,0)
			RArmAttach.Orientation = Vector3.new(0,0,0)
			RArmAttach:SetAttribute("Ragdoll", true)

			local torsoRArmAttach = Instance.new("Attachment")
			torsoRArmAttach.Parent = Torso
			torsoRArmAttach.Name = "TorsoRightArmAttachment"
			torsoRArmAttach.Position = Vector3.new(1,0.5,0)
			torsoRArmAttach.Orientation = Vector3.new(0,0,0)
			torsoRArmAttach:SetAttribute("Ragdoll", true)

			local RArmJoint = script.Default:Clone()
			RArmJoint.Parent = Torso
			RArmJoint.Name = "RightArmJoint"
			RArmJoint.Attachment0 = RArmAttach
			RArmJoint.Attachment1 = torsoRArmAttach
			RArmJoint:SetAttribute("Ragdoll", true)
			torsoRArmAttach:SetAttribute("Ragdoll", true)
			
			
			--Attaching Left Arm
			
			
			local LArmAttach = Instance.new("Attachment")
			LArmAttach.Parent = LeftArm
			LArmAttach.Name = "LeftArmAttachment"
			LArmAttach.Position = Vector3.new(0.5,0.5,0)
			LArmAttach.Orientation = Vector3.new(0,0,0)
			LArmAttach:SetAttribute("Ragdoll", true)

			local torsoLArmAttach = Instance.new("Attachment")
			torsoLArmAttach.Parent = Torso
			torsoLArmAttach.Name = "TorsoLeftArmAttachment"
			torsoLArmAttach.Position = Vector3.new(-1,0.5,0)
			torsoLArmAttach.Orientation = Vector3.new(0,0,0)
			torsoLArmAttach:SetAttribute("Ragdoll", true)

			local LArmJoint = script.Default:Clone()
			LArmJoint.Parent = Torso
			LArmJoint.Name = "LeftArmJoint"
			LArmJoint.Attachment0 = LArmAttach
			LArmJoint.Attachment1 = torsoLArmAttach
			LArmJoint:SetAttribute("Ragdoll", true)
			torsoLArmAttach:SetAttribute("Ragdoll", true)
			
			
			
			
			--Right Leg Attach
			
			
			
			
			local RLegAttach = Instance.new("Attachment")
			RLegAttach.Parent = RightLeg
			RLegAttach.Name = "RightLegAttachment"
			RLegAttach.Position = Vector3.new(0,1,0)
			RLegAttach.Orientation = Vector3.new(0,0,-90)
			RLegAttach:SetAttribute("Ragdoll", true)

			local torsoRLegAttach = Instance.new("Attachment")
			torsoRLegAttach.Parent = Torso
			torsoRLegAttach.Name = "TorsoRLegAttachment"
			torsoRLegAttach.Position = Vector3.new(-0.5,-1,0)
			torsoRLegAttach.Orientation = Vector3.new(0,0,-90)
			torsoRLegAttach:SetAttribute("Ragdoll", true)

			local RLegJoint = script.Default:Clone()
			RLegJoint.Parent = Torso
			RLegJoint.Name = "Joint"
			RLegJoint.Attachment0 = RLegAttach
			RLegJoint.Attachment1 = torsoRLegAttach
			RLegJoint:SetAttribute("Ragdoll", true)
			torsoRLegAttach:SetAttribute("Ragdoll", true)
			
			
			--LEFt Leg attach
			
			
			
			
			local LLegAttach = Instance.new("Attachment")
			LLegAttach.Parent = LeftLeg
			LLegAttach.Name = "LeftLegAttachment"
			LLegAttach.Position = Vector3.new(0,1,0)
			LLegAttach.Orientation = Vector3.new(0,0,-90)
			LLegAttach:SetAttribute("Ragdoll", true)

			local torsoLLegAttach = Instance.new("Attachment")
			torsoLLegAttach.Parent = Torso
			torsoLLegAttach.Name = "TorsoLLegAttachment"
			torsoLLegAttach.Position = Vector3.new(0.5,-1,0)
			torsoLLegAttach.Orientation = Vector3.new(0,0,-90)
			torsoLLegAttach:SetAttribute("Ragdoll", true)

			local LLegJoint = script.Default:Clone()
			LLegJoint.Parent = Torso
			LLegJoint.Name = "Joint"
			LLegJoint.Attachment0 = LLegAttach
			LLegJoint.Attachment1 = torsoLLegAttach
			LLegJoint:SetAttribute("Ragdoll", true)
			torsoLLegAttach:SetAttribute("Ragdoll", true)
			
		end	
		local plrcframe = Head.CFrame
		local bv = Instance.new("VectorForce")
		bv.Parent = Head
		bv.Force = Vector3.new(5,1,1)
		HumanoidRootPart.CanCollide  = false
		for i, v in pairs(char:GetChildren()) do
			if v:IsA("BasePart") and v.Name ~= "HumanoidRootPart" then
				local NC = Instance.new("NoCollisionConstraint")
				NC.Parent = Torso
				NC.Name = v.Name
				NC.Part0 = v
				NC.Part1 = Torso
				NC:SetAttribute("Ragdoll", true)
			end
		end	
	end
	for i, v in pairs(char:GetDescendants()) do
		if v:IsA("Motor6D") and v.Name ~= "Root" and v.Name ~= "RootJoint " then
			v.Enabled = false
			
		end
	end
end

function Ragdoll.Stand(character : model)
	local player = players:GetPlayerFromCharacter(character)
	if player then
		playerEvents:WaitForChild("Ragdoll"):FireClient(player, false, "Standing")
	end
	
	local hum = character:FindFirstChild("Humanoid")
	hum:FindFirstChild("HumanoidRootPart").CanCollide = false
	
	for i, v in pairs(character:GetDescendants()) do
		if v:GetAttribute("Ragdoll") then
			v:Destroy()
		end
		if v:IsA("Motor6D") then
			v.Enabled = true
		end
	end
end
return Ragdoll	

Here is the localScript

local replicatedstorage = game:GetService("ReplicatedStorage")
local players = game:GetService("Players")
local starterGui = game:GetService("StarterGui")
local cam = workspace.CurrentCamera
print(cam.CameraSubject)
local player = game.Players.LocalPlayer
local character = player.Character
local hum = character:WaitForChild("Humanoid")
local playerEvents = replicatedstorage:WaitForChild("Player")
starterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack, true)
wait(0.5)
function onRagdoll(Ragdolled)
	if Ragdolled then
		print('ragdollin')
		hum:UnequipTools()
		starterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack, false)
		hum:SetStateEnabled(Enum.HumanoidStateType.GettingUp, false)
		hum:ChangeState(Enum.HumanoidStateType.Physics)
		cam.CameraSubject = character.Head
	else
		print('standin')
		starterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack, true)
		hum:SetStateEnabled(Enum.HumanoidStateType.GettingUp, true)
		hum:ChangeState(Enum.HumanoidStateType.GettingUp)
		cam.CameraSubject = workspace:FindFirstChild(player.Name).Humanoid
	end
end


playerEvents.Ragdoll.OnClientEvent:Connect(onRagdoll)

And here is the regular script

local players = game:GetService("Players")
local replicatedFirst = game:GetService("ReplicatedFirst")
local module = require(game.ReplicatedFirst:WaitForChild("RagdollScript"))

players.PlayerAdded:Connect(function(plr)
	plr.CharacterAdded:Connect(function(chr)
		local hum = chr:WaitForChild("Humanoid")
		chr.HumanoidRootPart.AncestryChanged:Connect(function()
			if not chr:FindFirstChild("HumanoidRootPart") then wait(players.RespawnTime) plr:LoadCharacter() end		
		end)
		hum.BreakJointsOnDeath = false
		hum.RequiresNeck = false
		hum.Died:Connect(function()
			print("player is ded lol")
			module.RagdollCharacter(chr)
			print("player should have ragdolled")
		end)
	end)
end)

I’m looking for an explanation as to why this is happening. I believe it has something to do with the characters joints not breaking on death, but this is required for the ragdoll to work in the first place, so disabling it would defeat the whole point of the ragdoll script. I followed this tutorial when making these scripts.

One last thing, I would like this ragdoll script to be able to run whenever the module is called, not just when the player dies, so I cannot edit anything that would limit it to just the players’ death.

An explanation and/or a solution to this issue would be very helpful!

2 Likes