Guest User

RagdollClient (Local Script)

a guest
May 14th, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.47 KB | None | 0 0
  1. local character = script.Parent
  2.  
  3. function recurse(root,callback,i)
  4.     i= i or 0
  5.     for _,v in pairs(root:GetChildren()) do
  6.         i = i + 1
  7.         callback(i,v)
  8.        
  9.         if #v:GetChildren() > 0 then
  10.             i = recurse(v,callback,i)
  11.         end
  12.     end
  13.    
  14.     return i
  15. end
  16.  
  17. function ragdollJoint(part0, part1, attachmentName, className, properties)
  18.     attachmentName = attachmentName.."RigAttachment"
  19.     local constraint = Instance.new(className.."Constraint")
  20.     constraint.Attachment0 = part0:FindFirstChild(attachmentName)
  21.     constraint.Attachment1 = part1:FindFirstChild(attachmentName)
  22.     constraint.Name = "RagdollConstraint"..part1.Name
  23.    
  24.     for _,propertyData in next,properties or {} do
  25.         constraint[propertyData[1]] = propertyData[2]
  26.     end
  27.    
  28.     constraint.Parent = character
  29. end
  30.  
  31. function getAttachment0(attachmentName)
  32.     for _,child in next,character:GetChildren() do
  33.         local attachment = child:FindFirstChild(attachmentName)
  34.         if attachment then
  35.             return attachment
  36.         end
  37.     end
  38. end
  39.  
  40. character:WaitForChild("Humanoid").Died:connect(function()
  41.     local camera = workspace.CurrentCamera
  42.     if camera.CameraSubject == character.Humanoid then--If developer isn't controlling camera
  43.         camera.CameraSubject = character.UpperTorso
  44.     end
  45.  
  46.     --Make it so ragdoll can't collide with invisible HRP, but don't let HRP fall through map and be destroyed in process  
  47.     character.HumanoidRootPart.Anchored = true
  48.     character.HumanoidRootPart.CanCollide = false
  49.  
  50.     --Helps to fix constraint spasms
  51.     recurse(character, function(_,v)
  52.         if v:IsA("Attachment") then
  53.             v.Axis = Vector3.new(0, 1, 0)
  54.             v.SecondaryAxis = Vector3.new(0, 0, 1)
  55.             v.Rotation = Vector3.new(0, 0, 0)
  56.         end
  57.     end)
  58.    
  59.     --Re-attach hats
  60.     for _,child in next,character:GetChildren() do
  61.         if child:IsA("Accoutrement") then
  62.             --Loop through all parts instead of only checking for one to be forwards-compatible in the event
  63.             --ROBLOX implements multi-part accessories
  64.             for _,part in next,child:GetChildren() do
  65.                 if part:IsA("BasePart") then
  66.                     local attachment1 = part:FindFirstChildOfClass("Attachment")
  67.                     local attachment0 = getAttachment0(attachment1.Name)
  68.                     if attachment0 and attachment1 then
  69.                         --Shouldn't use constraints for this, but have to because of a ROBLOX idiosyncrasy where
  70.                         --joints connecting a character are perpetually deleted while the character is dead
  71.                         local constraint = Instance.new("HingeConstraint")
  72.                         constraint.Attachment0 = attachment0
  73.                         constraint.Attachment1 = attachment1
  74.                         constraint.LimitsEnabled = true
  75.                         constraint.UpperAngle = 0 --Simulate weld by making it difficult for constraint to move
  76.                         constraint.LowerAngle = 0
  77.                         constraint.Parent = character
  78.                     end
  79.                 end
  80.             end
  81.         end
  82.     end
  83.    
  84.     ragdollJoint(character.LowerTorso, character.UpperTorso, "Waist", "BallSocket", {
  85.         {"LimitsEnabled",true};
  86.         {"UpperAngle",5};
  87.     })
  88.     ragdollJoint(character.UpperTorso, character.Head, "Neck", "BallSocket", {
  89.         {"LimitsEnabled",true};
  90.         {"UpperAngle",15};
  91.     })
  92.    
  93.     local handProperties = {
  94.         {"LimitsEnabled", true};
  95.         {"UpperAngle",0};
  96.         {"LowerAngle",0};
  97.     }
  98.     ragdollJoint(character.LeftLowerArm, character.LeftHand, "LeftWrist", "Hinge", handProperties)
  99.     ragdollJoint(character.RightLowerArm, character.RightHand, "RightWrist", "Hinge", handProperties)
  100.    
  101.     local shinProperties = {
  102.         {"LimitsEnabled", true};
  103.         {"UpperAngle", 0};
  104.         {"LowerAngle", -75};
  105.     }
  106.     ragdollJoint(character.LeftUpperLeg, character.LeftLowerLeg, "LeftKnee", "Hinge", shinProperties)
  107.     ragdollJoint(character.RightUpperLeg, character.RightLowerLeg, "RightKnee", "Hinge", shinProperties)
  108.    
  109.     local footProperties = {
  110.         {"LimitsEnabled", true};
  111.         {"UpperAngle", 15};
  112.         {"LowerAngle", -45};
  113.     }
  114.     ragdollJoint(character.LeftLowerLeg, character.LeftFoot, "LeftAnkle", "Hinge", footProperties)
  115.     ragdollJoint(character.RightLowerLeg, character.RightFoot, "RightAnkle", "Hinge", footProperties)
  116.    
  117.     --TODO fix ability for socket to turn backwards whenn ConeConstraints are shipped
  118.     ragdollJoint(character.UpperTorso, character.LeftUpperArm, "LeftShoulder", "BallSocket")
  119.     ragdollJoint(character.LeftUpperArm, character.LeftLowerArm, "LeftElbow", "BallSocket")
  120.     ragdollJoint(character.UpperTorso, character.RightUpperArm, "RightShoulder", "BallSocket")
  121.     ragdollJoint(character.RightUpperArm, character.RightLowerArm, "RightElbow", "BallSocket")
  122.     ragdollJoint(character.LowerTorso, character.LeftUpperLeg, "LeftHip", "BallSocket")
  123.     ragdollJoint(character.LowerTorso, character.RightUpperLeg, "RightHip", "BallSocket")
  124. end)
Add Comment
Please, Sign In to add comment