Hello, I have an issue with my script that allows players to get ragdolled (while still being controllable) and then unragdolled.
local Ragdoll = {}
function setJoints(player : Player, jointType : string)
local character = player.Character
local humanoid : Humanoid = character:FindFirstChild("Humanoid")
humanoid.BreakJointsOnDeath = false
humanoid.RequiresNeck = false
local descendants = character:GetDescendants()
for i, object in descendants do
if object:IsA(jointType == "BallSocketConstraint" and "Motor6D" or "BallSocketConstraint") then
if object.Parent.Name ~= "HumanoidRootPart" then
local ballSocketConstraint = Instance.new(jointType)
ballSocketConstraint.Parent = object.Parent
if jointType ~= "Motor6D" then
local attachment0 = Instance.new("Attachment")
local attachment1 = Instance.new("Attachment")
attachment0.Parent = object.Part0
attachment1.Parent = object.Part1
ballSocketConstraint.Attachment0 = attachment0
ballSocketConstraint.Attachment1 = attachment1
attachment0.CFrame = object.C0
attachment1.CFrame = object.C1
else
if object:GetAttribute("Attachment0") ~= nil then
ballSocketConstraint.C0 = object.Attachment0.CFrame
ballSocketConstraint.C1 = object.Attachment1.CFrame
ballSocketConstraint.Part0 = object.Attachment0.Parent
ballSocketConstraint.Part1 = object.Attachment1.Parent
end
end
end
object:Destroy()
end
end
end
function removeHumanoidRootPartAttachments(player : Player)
local character = player.Character
local humanoidRootPart : BasePart = character:FindFirstChild("HumanoidRootPart")
if humanoidRootPart ~= nil then
for i,v in ipairs(humanoidRootPart:GetDescendants()) do
if v:IsA("Sound") ~= true then
v:Destroy()
end
end
end
end
function attachHumanoidRootPartToRagdoll(player : Player)
local humanoidRootPart : BasePart = player.Character.HumanoidRootPart
local spring = Instance.new("SpringConstraint")
local attachment0 = Instance.new("Attachment")
attachment0.Parent = humanoidRootPart
local attachment1 = Instance.new("Attachment")
attachment1.Parent = player.Character.Head
spring.Attachment0 = attachment0
spring.Attachment1 = attachment1
spring.LimitsEnabled = true
spring.MaxLength = 3.5
spring.Parent = humanoidRootPart
spring.Name = "Spring"
end
function restoreHumanoidRootPart(player : Player)
local humanoidRootPart = player.Character.HumanoidRootPart
local spring : SpringConstraint = humanoidRootPart.Spring
local humanoid : Humanoid = player.Character.Humanoid
spring.Attachment0:Destroy()
spring.Attachment1:Destroy()
spring:Destroy()
for i, v in ipairs(player.Character:GetDescendants()) do
if v:IsA("BasePart") then
v.CFrame = CFrame.new(v.CFrame.Position)
v.Anchored = true
v.CanCollide = false
end
end
local torso = player.Character:FindFirstChild(humanoid.RigType == Enum.HumanoidRigType.R6 and "Torso" or "UpperTorso")
local motor6d : Motor6D = Instance.new("Motor6D")
motor6d.Part0 = torso
motor6d.Part1 = humanoidRootPart
motor6d.Parent = humanoidRootPart
humanoidRootPart.CFrame = CFrame.new(torso.CFrame.Position)
humanoidRootPart.CanCollide = false
-- Disable rotations
for i, v in ipairs(player.Character:GetDescendants()) do
if v:IsA("BasePart") then
v.CFrame = CFrame.new(v.CFrame.Position)
v.Anchored = false
end
end
end
function Ragdoll.unragdollPlayer(player : Player)
setJoints(player, "Motor6D")
local humanoid : Humanoid = player.Character.Humanoid
humanoid:ChangeState(Enum.HumanoidStateType.Running)
player.Character.isRagdolled.Value = 0
player.Character.isRagdolled.Value = false
restoreHumanoidRootPart(player)
player.Character.HumanoidRootPart.CanCollide = false
end
function setBodyCollisions(player : Player, value)
for i, v in ipairs(player.Character:GetDescendants()) do
if v:IsA("BasePart") == true then
v.CanCollide = value
end
end
end
function Ragdoll.ragdollPlayer(player : Player)
setJoints(player, "BallSocketConstraint")
local isRagdolled = player.Character:FindFirstChild("isRagdolled")
if isRagdolled == nil then
isRagdolled = Instance.new("BoolValue")
isRagdolled.Parent = player.Character
isRagdolled.Name = "isRagdolled"
end
if isRagdolled == true then
return
end
local humanoid : Humanoid = player.Character.Humanoid
player.Character.isRagdolled.Value = true
removeHumanoidRootPartAttachments(player)
attachHumanoidRootPartToRagdoll(player)
setBodyCollisions(player, true)
end
return Ragdoll
The issue shown in a screenshot:
What I tried so far?
Try to make cancollide true but not only that doesn’t really tackle the issue but also introduces a problem where player is not able to swim (related to density, which can be changed but why can’t I keep them in their default?)