Hello! I am making a public ModuleScript of Ragdoll, which me are trying to make it the more faster possible. Anyone haves a tip of how can I improve the code? any suggest to change the code is very welcome!
- You can get the entire module here: WRagdoll Module [R15] v1.2 - Roblox
Main Module:
local module = {}
local Limits = require(script.Joints)
function module:TurnRagdoll(Humanoid)
assert(Humanoid and Humanoid:IsA("Humanoid"), "'Humanoid' parameter is not a humanoid!")
local Character = Humanoid.Parent
local isragdoll = Humanoid:GetState() == Enum.HumanoidStateType.Ragdoll
Humanoid:SetStateEnabled(Enum.HumanoidStateType.GettingUp, isragdoll)
Humanoid:ChangeState(not isragdoll and Enum.HumanoidStateType.Ragdoll or Enum.HumanoidStateType.GettingUp)
local Attachments = {}
for _, obj in ipairs(Character:GetDescendants()) do
if obj:IsA("Attachment") and string.find(obj.Name, "RigAttachment") and obj.Parent ~= Character.PrimaryPart then
if not Attachments[obj.Name] then
Attachments[obj.Name] = obj
elseif not isragdoll then
local Attach0 = Attachments[obj.Name]
local Attach1 = obj
local BallSocket = Instance.new("BallSocketConstraint")
BallSocket.Attachment0 = Attach1
BallSocket.Attachment1 = Attach0
local register = Limits[obj.Name]
BallSocket.MaxFrictionTorque = register and register.MaxFrictionTorque or 2
BallSocket.LimitsEnabled = true
BallSocket.UpperAngle = register and register.UpperAngle or 30
BallSocket.Restitution = Limits["Restituition"] or 0
BallSocket.TwistLimitsEnabled = true
BallSocket.TwistLowerAngle = register and register.TwistLowerAngle or -60
BallSocket.TwistUpperAngle = register and register.TwistUpperAngle or 60
BallSocket.Parent = Attach1.Parent
else
for _, inst in ipairs(obj.Parent:GetChildren()) do
if inst:IsA("Constraint") then
inst:Destroy()
end
end
end
obj.Parent.CanCollide = true
elseif obj:IsA("Motor6D") then
obj.Enabled = isragdoll
end
end
Character.PrimaryPart.CanCollide = isragdoll
local Weld = Character.PrimaryPart:FindFirstChild("RagdollWeld")
if not Weld then
Weld = Instance.new("WeldConstraint")
Weld.Name = "RagdollWeld"
Weld.Part0 = Character.UpperTorso
Weld.Part1 = Character.PrimaryPart
Weld.Parent = Character.PrimaryPart
else
Weld:Destroy()
end
end
function module:IsRagdoll(Humanoid)
return Humanoid.Parent.PrimaryPart:FindFirstChild("RagdollWeld") ~= nil
end
return module
Joints (configuration module):
--[[
This module is a configuration for the main "Wo's Ragdoll" module!
Here, you can modify each body joint's malleability on Ragdoll mode
]]
local mft1, mft2, mft3 = 0, 0, 20
local ua1, ua2, ua3 = 100, 40, 5
local tla1, tla2, tla3 = -45, -20, -10
local tua1, tua2, tua3 = 45, 20, 10
local Limits = {
Restituition = .93,
--[[
RIGHT ARM
]]
RightShoulderRigAttachment = {
MaxFrictionTorque = mft1,
UpperAngle = ua1,
TwistLowerAngle = tla1,
TwistUpperAngle = tua1,
},
RightElbowRigAttachment = {
MaxFrictionTorque = mft2,
UpperAngle = ua2,
TwistLowerAngle = tla2,
TwistUpperAngle = tua2,
},
RightWristRigAttachment = {
MaxFrictionTorque = mft3,
UpperAngle = ua3,
TwistLowerAngle = tla3,
TwistUpperAngle = tua3,
},
--[[
LEFT ARM
]]
LeftShoulderRigAttachment = {
MaxFrictionTorque = mft1,
UpperAngle = ua1,
TwistLowerAngle = tla1,
TwistUpperAngle = tua1,
},
LeftElbowRigAttachment = {
MaxFrictionTorque = mft2,
UpperAngle = ua2,
TwistLowerAngle = tla2,
TwistUpperAngle = tua2,
},
LeftWristRigAttachment = {
MaxFrictionTorque = mft3,
UpperAngle = ua3,
TwistLowerAngle = tla3,
TwistUpperAngle = tua3,
},
--[[
RIGHT LEG
]]
RightHipRigAttachment = {
MaxFrictionTorque = mft1,
UpperAngle = ua1,
TwistLowerAngle = tla1,
TwistUpperAngle = tua1,
},
RightKneeRigAttachment = {
MaxFrictionTorque = mft2,
UpperAngle = ua2,
TwistLowerAngle = tla2,
TwistUpperAngle = tua2,
},
RightAnkleRigAttachment = {
MaxFrictionTorque = mft3,
UpperAngle = ua3,
TwistLowerAngle = tla3,
TwistUpperAngle = tua3,
},
--[[
LEFT LEG
]]
LeftHipRigAttachment = {
MaxFrictionTorque = mft1,
UpperAngle = ua1,
TwistLowerAngle = tla1,
TwistUpperAngle = tua1,
},
LeftKneeRigAttachment = {
MaxFrictionTorque = mft2,
UpperAngle = ua2,
TwistLowerAngle = tla2,
TwistUpperAngle = tua2,
},
LeftAnkleRigAttachment = {
MaxFrictionTorque = mft3,
UpperAngle = ua3,
TwistLowerAngle = tla3,
TwistUpperAngle = tua3,
},
--[[
NECK
]]
NeckRigAttachment = {
MaxFrictionTorque = 0,
UpperAngle = 15,
TwistLowerAngle = -50,
TwistUpperAngle = 50,
},
}
return Limits