Ragdoll script not working on player death specifically

  1. What do you want to achieve? Keep it simple and clear!

Ragdoll script’s only working when the function is called via remote event when the player is alive but breaks when trying to call on humanoid.died even though the function is called correctly and there is a response on console output when the player dies.

I’ve disabled humanoid.breakjointsondeath so its not that.

  1. What is the issue? Include screenshots / videos if possible!

In this case, when calling a remote event where the player gets ragdolled whilst alive it works.

But it doesn’t work when the player dies

local Players = game:GetService("Players")
local UserInputService = game:GetService("UserInputService")
local player = Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local ragdolling = false
local motors = {}

local remote = game.ReplicatedStorage.Rag

local function storeMotors()
	for _, motor in pairs(character:GetDescendants()) do
		if motor:IsA("Motor6D") then
			motors[motor] = {C0 = motor.C0, C1 = motor.C1, Part0 = motor.Part0, Part1 = motor.Part1}
		end
	end
end

local function enableRagdoll()
	print("ragging it")
	if not ragdolling then
		ragdolling = true
		humanoid:ChangeState(Enum.HumanoidStateType.Physics)

		for _, motor in pairs(character:GetDescendants()) do
			if motor:IsA("Motor6D") then
				local socket = Instance.new("BallSocketConstraint")
				local a0 = Instance.new("Attachment")
				local a1 = Instance.new("Attachment")
				a0.CFrame = motor.C0
				a1.CFrame = motor.C1
				a0.Parent = motor.Part0
				a1.Parent = motor.Part1
				socket.Attachment0 = a0
				socket.Attachment1 = a1
				socket.Parent = motor.Part1.Parent

				motor.Part1.Velocity = Vector3.new(0, 0, 0)
				motor.Part1.RotVelocity = Vector3.new(0, 0, 0)

				motor.Enabled = false
				motor.Part0 = nil
				motor.Part1 = nil
			end
		end
	end
end

local function disableRagdoll()
	if ragdolling then
		ragdolling = false
		humanoid:ChangeState(Enum.HumanoidStateType.GettingUp)

		for motor, state in pairs(motors) do
			if motor.Parent then
				motor.Part0 = state.Part0
				motor.Part1 = state.Part1
				motor.C0 = state.C0
				motor.C1 = state.C1
				motor.Enabled = true
			end
		end

		for _, part in pairs(character:GetDescendants()) do
			if part:IsA("BallSocketConstraint") then
				part:Destroy()
			elseif part:IsA("Attachment") then
				part:Destroy()
			end
		end
	end
end

local function onInputBegan(input, gameProcessed)
	if not gameProcessed and input.KeyCode == Enum.KeyCode.Space then
		if ragdolling then
			disableRagdoll()
		else
		end
	end
end

storeMotors()

UserInputService.InputBegan:Connect(onInputBegan)

remote.OnClientEvent:Connect(enableRagdoll)

humanoid.Died:Connect(enableRagdoll)

Update: Looks like the joints are getting frozen a few seconds after the player dies but there is a little bit of ragdolling on death.