I made a ragdoll module recently and it feels really scuffed because it takes advantage of the r15 player characters naming convention is. I wanna know if there was a more reliable way of copying Motor6Ds into BallSocketConstraints.
Oh, I also want info on whether any other parts of my code could be further optimized.
MODULE
local RagSys = {}
local Players = game:GetService("Players")
local function BuildRagdollJoints(char:Model)
char:SetAttribute("Ragdoll", false)
print("Building Joints")
for _,v in pairs(char:GetChildren()) do
if v:IsA("MeshPart") then
print(v)
local CollisionPart = Instance.new("Part")
CollisionPart.Parent = v
CollisionPart.CFrame = v.CFrame
CollisionPart.Size = v.Size/2
CollisionPart.Massless = true
CollisionPart.Transparency = 0
CollisionPart.Name = "COLLISION_PART"
CollisionPart.CanCollide = false
local Weld = Instance.new("WeldConstraint",CollisionPart)
Weld.Part0 = CollisionPart
Weld.Part1 = v
end
end
for _,v in pairs(char:GetDescendants()) do
if v:IsA("Motor6D") then
local Joint = v
if Joint.Name == "Neck" or Joint.Name == "Root" then continue end
--print("Building Joints for: ")
--print(Joint)
local BallJoint = Instance.new("BallSocketConstraint")
local Part0 = nil
local Part1 = nil
for _,v in pairs(Joint.Part0:GetChildren()) do
local Attachment = v
if string.match(v.Name, Joint.Name) ~= nil then
print(v)
Part0 = v
end
end
for _,v in pairs(Joint.Part1:GetChildren()) do
local Attachment = v
if v:IsA("Attachment") and string.match(v.Name, Joint.Name) ~= nil then
print(v)
Part1 = v
end
end
BallJoint.Name = "RAGDOLL_CONSTRAINT"
BallJoint.Parent = Joint.Parent
BallJoint.Attachment0 = Part0
BallJoint.Attachment1 = Part1
BallJoint.LimitsEnabled = true
BallJoint.TwistLimitsEnabled = true
end
end
end
local function EnableMotor6Ds(char:Model, enabled:boolean)
for _,Motor6D in pairs(char:GetDescendants()) do
if Motor6D:IsA("Motor6D") == false then continue end
if Motor6D.Name == "Neck" or Motor6D.Name == "Root" then continue end
Motor6D.Enabled = enabled
end
end
local function EnableBallConstraints(char:Model, enabled:boolean)
for _,BallConstraint in pairs(char:GetDescendants()) do
if BallConstraint:IsA("BallSocketConstraint") == false then continue end
BallConstraint.Enabled = enabled
end
end
local function EnableCollisionParts(char:Model, enabled:boolean)
for _,v in pairs(char:GetDescendants()) do
if v.Name == "COLLISION_PART" then
v.CanCollide = enabled
end
end
end
function RagSys.Init()
print("Initializing")
Players.PlayerAdded:Connect(function(plr)
print("Player was added")
plr.CharacterAdded:Connect(function(char)
print("Char added")
local Humanoid: Humanoid = char:FindFirstChild("Humanoid")
Humanoid.BreakJointsOnDeath = false
Humanoid.RequiresNeck = false
BuildRagdollJoints(char)
end)
end)
end
function RagSys.RagdollChar(char:Model, duration:number)
EnableMotor6Ds(char, false)
EnableBallConstraints(char, true)
EnableCollisionParts(char, true)
char:FindFirstChild("HumanoidRootPart").CanCollide = false
char:FindFirstChildOfClass("Humanoid").PlatformStand = true
if duration ~= nil then
task.spawn(function()
task.wait(duration)
RagSys.UnragdollChar(char)
end)
end
end
function RagSys.UnragdollChar(char:Model)
EnableMotor6Ds(char, true)
EnableBallConstraints(char, false)
EnableCollisionParts(char, false)
char:FindFirstChild("HumanoidRootPart").CanCollide = true
char:FindFirstChildOfClass("Humanoid").PlatformStand = false
end
return RagSys
SERVER SCRIPT
local RepStorage = game:GetService("ReplicatedStorage")
local Remotes = RepStorage.Remotes
RagSys = require(script.Parent.RagdollSystem)
-- INITALIZATION =========================================
RagSys.Init()
-- REMOTES ===============================================
Remotes.Ragdoll.OnServerEvent:Connect(function(plr)
local char = plr.Character
if char:GetAttribute("Ragdolled") == true then
RagSys.UnragdollChar(char)
else
RagSys.RagdollChar(char)
end
char:SetAttribute("Ragdolled", not char:GetAttribute("Ragdolled"))
end)
-- MISCELLANEOUS SETUP ===================================
wait(5)
RagSys.RagdollChar(game:GetService("Players"):WaitForChild("Renoitas").Character, 3)