Hello, I put together with AI code together for ragdoll but it’s all weird. Instead of normal ragdoll, all the limbs beside torso clips through ground and head moves in random direction at the speed of light. I’m trying to make it like a normal ragdoll when you just fall without any limb clipping through anything and a bit realistic so the limbs don’t bend unnaturally. I also encounter rare bug when the player gets up, they will just stand on their head and the only way to get unstuck is to reset.
I want to make this work for R6, I already tried R15 too.
The code:
local ragdoll = {}
ragdoll.Constraints = {}
function ragdoll.Start(character)
if character.Ragdoll.Value then return end
character.Ragdoll.Value = true
local physicsService = game:GetService("PhysicsService")
local collisionGroupName = "RagdollCollisionGroup"
if not physicsService:IsCollisionGroupRegistered(collisionGroupName) then
physicsService:RegisterCollisionGroup(collisionGroupName)
end
physicsService:CollisionGroupSetCollidable(collisionGroupName, collisionGroupName, true)
physicsService:CollisionGroupSetCollidable(collisionGroupName, "Default", true)
local torso = character:FindFirstChild("Torso")
if torso then
torso.CollisionGroup = collisionGroupName
torso.Anchored = true
end
for _, joint in pairs(character:GetDescendants()) do
if joint:IsA("Motor6D") then
local part0 = joint.Part0
local part1 = joint.Part1
local c0 = joint.C0
local c1 = joint.C1
local att0 = Instance.new("Attachment", part0)
local att1 = Instance.new("Attachment", part1)
att0.CFrame = c0
att1.CFrame = c1
local newConstraint
if joint.Name == "Neck" or joint.Name == "Left Shoulder" or joint.Name == "Right Shoulder" then
newConstraint = Instance.new("BallSocketConstraint")
newConstraint.Attachment0 = att0
newConstraint.Attachment1 = att1
newConstraint.LimitsEnabled = true
newConstraint.TwistLimitsEnabled = true
newConstraint.UpperAngle = 45
elseif joint.Name == "Left Hip" or joint.Name == "Right Hip" then
newConstraint = Instance.new("HingeConstraint")
newConstraint.Attachment0 = att0
newConstraint.Attachment1 = att1
newConstraint.LimitsEnabled = true
newConstraint.LowerAngle = -30
newConstraint.UpperAngle = 30
else
newConstraint = Instance.new("BallSocketConstraint")
newConstraint.Attachment0 = att0
newConstraint.Attachment1 = att1
newConstraint.LimitsEnabled = true
newConstraint.TwistLimitsEnabled = true
newConstraint.UpperAngle = 30
end
if newConstraint then
newConstraint.Parent = part0
table.insert(ragdoll.Constraints, newConstraint)
end
joint.Enabled = false
part0.CollisionGroup = collisionGroupName
part1.CollisionGroup = collisionGroupName
part0.CanCollide = true
part1.CanCollide = true
elseif joint:IsA("BasePart") then
joint.CollisionGroup = collisionGroupName
joint.CanCollide = true
end
end
local humanoid = character:FindFirstChild("Humanoid")
if humanoid then
humanoid.WalkSpeed = 0
humanoid.JumpPower = 0
humanoid.PlatformStand = true
humanoid.AutoRotate = false
end
if torso then
torso.Anchored = false
end
end
function ragdoll.Stop(character)
local humanoid = character:FindFirstChild("Humanoid")
if humanoid then
humanoid.PlatformStand = false
end
for _, constraint in ipairs(ragdoll.Constraints) do
if constraint then
constraint:Destroy()
end
end
ragdoll.Constraints = {}
for _, joint in pairs(character:GetDescendants()) do
if joint:IsA("Motor6D") then
joint.Enabled = true
end
end
character.Ragdoll.Value = false
if humanoid then
humanoid:ChangeState(Enum.HumanoidStateType.GettingUp)
humanoid.WalkSpeed = 16
humanoid.JumpPower = 50
humanoid.AutoRotate = true
end
end
return ragdoll