Knockdown ragdoll script errors

Im trying to develop a ragdoll script for r6. If the player’s hp reaches below .1, the player gets knocked and recovers in 15 seconds. This ragdoll works fine upon death, but does not work if the player is still alive. Does anybody know how to fix this?

local RagdollClientEvent = game.ReplicatedStorage:WaitForChild(“Events”).Ragdoll

local function unragdoll(player)
local character = player.Character
for _,v in pairs(character:GetDescendants()) do --unragdoll
if v:IsA(‘Motor6D’) then
v.Enabled = true
end
if v.Name == ‘BallSocketConstraint’ then
v:Destroy()
end
if v.Name == ‘Attachment’ then
v:Destroy()
end
end
RagdollClientEvent:FireClient(player)
end

local function ragdollplayer(player)
local character = player.Character
for _, v in pairs(character:GetDescendants()) do --ragdoll
if v:IsA(“Motor6D”) then
local a0, a1 = Instance.new(“Attachment”), Instance.new(“Attachment”)
a0.CFrame = v.C0
a1.CFrame = v.C1
a0.Parent = v.Part0
a1.Parent = v.Part1
local b = Instance.new(“BallSocketConstraint”)
b.Attachment0 = a0
b.Attachment1 = a1
b.Parent = v.Part0
v.Enabled = false
end
RagdollClientEvent:FireClient(player)
end
wait(15)
unragdoll(player)
end

local function LoadPlayer(player)
local char = player.Character or player.CharacterAdded:Wait()
local hum = char.Humanoid

hum.HealthChanged:connect(function()
	if hum.Health < 1 then
		hum.Health = math.clamp(.5,.01,1)
		ragdollplayer(player)
	end
end)

end

game.Players.PlayerAdded:connect(function(player)

LoadPlayer(player)

end)

This seems a very old post but whatever.

Imma say there’s a lot of problems here.
So first, please use devforum’s preformatted text option to keep your code clear with indents, to do that wrap your code with ``` ,
for example:

--example code--
local example = function ()
	print("Please wrap your code with ```")
end
example()

But next, even I haven’t gone into motor6Ds deep, I do can give you some general coding tips and practice to follow.

In your code you used things like
local b = Instance.new("BallSocketConstraint")
that will run every single time the code runs, which will create a lot of duplicated instances in it’s parent and may cause bugs with referencing, also naming variables with “a0”,“a1” or “b” will never be a good practice, which might confuse you in the future.

Despite the naming issue, to deal with this, I recommend you code a mainModule function like the following:

local InstanceManager = {}
local self = InstanceManager
local debris = game:GetService("Debris")

InstanceManager.GetInstance = function (Type,Parent,Name,PropertyList,LifeTime)
	local InstanceToGet = Parent:FindFirstChild(Name) or Instance.new(Type)
	InstanceToGet.Name = Name
	if PropertyList then
		for property,value in pairs(PropertyList) do
			InstanceToGet[property] = value
		end
	end
	if LifeTime then
		debris:AddItem(InstanceToGet,LifeTime)
	end

	InstanceToGet.Parent = Parent
	--yes I recommend parenting new objects at last to prevent some issues
	return InstanceToGet
end

and in your case instead of doing

local a0, a1 = Instance.new(“Attachment”), Instance.new(“Attachment”)
a0.CFrame = v.C0
a1.CFrame = v.C1
a0.Parent = v.Part0
a1.Parent = v.Part1

with the module function you can do

local InstanceManager = require(ReplicatedStorage.Example.InstanceManager)

local a0 = InstanceManager.GetInstance("Attachment",v.Part0,"A0",{CFrame=v.C0})
--well you can add a lifetime to it actually, by adding a last param, for example
--if you want the attachments to destroy after the 15 second cooldown for whatever
--reason, you can add 15 after the list {CFrame=v.C0}
local a1 = InstanceManager.GetInstance("Attachment",v.Part1,"A1",{CFrame=v.C1})

I’m not sure if this even helps, one this is a post from 2 years ago and I’m not sure if you still have that passion on developing, if you do, congrats and keep going! Second this one really doesn’t deal with motor6Ds and may not fix the problem at all, but still if you may, remember this piece of advice and your developing career (hopefully) will be better!

If you see this and got as much as a little help, I will be very happy!