Skip to content

Instantly share code, notes, and snippets.

@MaximumADHD
Last active November 13, 2020 15:31
Show Gist options
  • Save MaximumADHD/a67d7370fdd890672fc71e84c825e86b to your computer and use it in GitHub Desktop.
Save MaximumADHD/a67d7370fdd890672fc71e84c825e86b to your computer and use it in GitHub Desktop.
Ragdoll.lua
local ragdollJoints =
{
Ankle =
{
Type = "BallSocket";
Properties =
{
UpperAngle = 20;
TwistUpperAngle = 20;
LimitsEnabled = true;
TwistLimitsEnabled = true;
};
};
Wrist =
{
Type = "BallSocket";
Properties =
{
UpperAngle = 10;
TwistUpperAngle = 5;
TwistLowerAngle = -5;
LimitsEnabled = true;
TwistLimitsEnabled = true;
}
};
Shoulder =
{
Type = "BallSocket";
Properties =
{
UpperAngle = 170;
LimitsEnabled = true;
}
};
Hip =
{
Type = "BallSocket";
AngleAdjust = CFrame.Angles(
0,
-math.rad(90),
math.rad(90)
);
Properties =
{
UpperAngle = 135;
TwistUpperAngle = 3;
TwistLowerAngle = -3;
LimitsEnabled = true;
TwistLimitsEnabled = true;
}
};
Elbow =
{
Type = "Hinge";
Properties =
{
LowerAngle = 0;
UpperAngle = 135;
LimitsEnabled = true;
};
};
Knee =
{
Type = "Hinge";
Properties =
{
UpperAngle = -10;
LowerAngle = -135;
LimitsEnabled = true;
};
};
Waist =
{
Type = "BallSocket";
Properties =
{
UpperAngle = 30;
LimitsEnabled = true;
TwistLimitsEnabled = true;
};
};
Neck =
{
Type = "BallSocket";
Properties =
{
UpperAngle = 30;
LimitsEnabled = true;
TwistLimitsEnabled = true;
};
};
}
local character = script.Parent
local humanoid = character:WaitForChild("Humanoid")
local function onDied()
local bones = {}
for _,desc in pairs(character:GetDescendants()) do
if desc:IsA("Motor6D") then
local name = desc.Name
if name:sub(1, 4) == "Left" then
name = name:sub(5)
elseif name:sub(1, 5) == "Right" then
name = name:sub(6)
end
bones[desc] = name
end
end
for bone, jointType in pairs(bones) do
local ragdollData = ragdollJoints[jointType]
local part0 = bone.Part0
local part1 = bone.Part1
if ragdollData and part0 and part1 then
local jointName = bone.Name .. "RigAttachment"
local a0 = part0:FindFirstChild(jointName)
local a1 = part1:FindFirstChild(jointName)
if a0 and a1 then
local cf = part1.CFrame
local adjust = ragdollData.AngleAdjust
local ragdollType = ragdollData.Type .. "Constraint"
local ragdollJoint = Instance.new(ragdollType)
ragdollJoint.Restitution = 0.75
ragdollJoint.Name = bone.Name
for prop, value in pairs(ragdollData.Properties) do
ragdollJoint[prop] = value
end
if adjust then
a0.CFrame = a0.CFrame * adjust
a1.CFrame = a1.CFrame * adjust
end
ragdollJoint.Attachment0 = a0
ragdollJoint.Attachment1 = a1
ragdollJoint.Parent = bone.Parent
bone:Destroy()
part1.CFrame = cf
end
end
end
local rootPart = humanoid.RootPart
if rootPart then
rootPart:Destroy()
end
humanoid.AutoRotate = false
end
humanoid.BreakJointsOnDeath = false
humanoid.Died:Connect(onDied)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment