I am using a module to create a ragdoll system. In the middle of testing, I ran into two issues:
- Upon death, the player floats after hitting the floor.
- The death sound is being played more than once.
This is the following script so far.
local CharacterModule = {}
function CharacterModule.Ragdoll(Character: Model, Died: boolean, Duration: number)
local Humanoid = Character.Humanoid
Humanoid:ChangeState(Enum.HumanoidStateType.Physics)
local StoredJoints = {}
for index, Joint in pairs(Character:GetDescendants()) do
if Joint:IsA("Motor6D") then
local Attachment0 = Instance.new("Attachment")
Attachment0.Name = "BallSocketAttachment0"
Attachment0.Parent = Joint.Part0
Attachment0.CFrame = Joint.C0
local Attachment1 = Instance.new("Attachment")
Attachment1.Name = "BallSocketAttachment1"
Attachment1.Parent = Joint.Part1
Attachment1.CFrame = Joint.C1
local BallSocketConstraint = Instance.new("BallSocketConstraint")
BallSocketConstraint.Attachment0 = Attachment0
BallSocketConstraint.Attachment1 = Attachment1
BallSocketConstraint.Name = "RagdollConstraint"
BallSocketConstraint.Parent = Joint.Parent
Joint.Parent.CanCollide = false
if not Died then
Joint.Enabled = false
table.insert(StoredJoints, Joint)
else
Joint:Destroy()
end
end
end
if not Died then
task.delay(Duration, function()
for _, Constraint in pairs(Character:GetDescendants()) do
if Constraint:IsA("BallSocketConstraint") or Constraint:IsA("Attachment") then
if Constraint.Name:match("Ragdoll") then
Constraint:Destroy()
end
end
end
for _, Joint in ipairs(StoredJoints) do
if Joint and Joint.Parent then
Joint.Enabled = true
end
Joint.Parent.CanCollide = true
end
end)
else
for _, Limb in pairs(Character:GetChildren()) do
if Limb:IsA("BasePart") and Limb:CanSetNetworkOwnership() then
local Player = Players:GetPlayerFromCharacter(Character)
if Player then
Limb:SetNetworkOwner(Player) --Prevents lag upon death.
end
end
end
Camera.CameraSubject = Character:FindFirstChild("Torso")
end
end
return CharacterModule