Advertisement
Guest User

Untitled

a guest
Jul 25th, 2019
513
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.72 KB | None | 0 0
  1. --Put in ServerScriptService
  2.  
  3. local welds = {}
  4. local constraints = {}
  5.  
  6. function recurse(root,callback,i)
  7. i= i or 0
  8. for _,v in pairs(root:GetChildren()) do
  9. i = i + 1
  10. callback(i,v)
  11.  
  12. if #v:GetChildren() > 0 then
  13. i = recurse(v,callback,i)
  14. end
  15. end
  16.  
  17. return i
  18. end
  19.  
  20. function ragdollJoint(character, part0, part1, attachmentName, className, properties)
  21. attachmentName = attachmentName.."RigAttachment"
  22. local constraint = Instance.new(className.."Constraint")
  23. constraint.Attachment0 = part0:FindFirstChild(attachmentName)
  24. constraint.Attachment1 = part1:FindFirstChild(attachmentName)
  25. constraint.Name = "RagdollConstraint"..part1.Name
  26.  
  27. for _,propertyData in next,properties or {} do
  28. constraint[propertyData[1]] = propertyData[2]
  29. end
  30.  
  31. constraint.Parent = character
  32. table.insert(constraints, constraint)
  33. end
  34.  
  35. function getAttachment0(character, attachmentName)
  36. for _,child in next,character:GetChildren() do
  37. local attachment = child:FindFirstChild(attachmentName)
  38. if attachment then
  39. return attachment
  40. end
  41. end
  42. end
  43.  
  44. function ragdoll(character)
  45. --character.HumanoidRootPart.Velocity = Vector3.new(0,0,0)
  46. for index, child in pairs(character:GetDescendants()) do
  47. if child.ClassName == "Motor6D" then
  48. if child.Name ~= "Neck" then
  49. table.insert(welds, {w = child, p = child.Parent})
  50. end
  51. end
  52. end
  53.  
  54. for i,v in pairs(welds) do
  55. print("Disabling character")
  56. v.w.Parent = nil
  57. end
  58.  
  59. local camera = workspace.CurrentCamera
  60. if camera.CameraSubject == character.Humanoid then--If developer isn't controlling camera
  61. camera.CameraSubject = character.UpperTorso
  62. end
  63.  
  64. --Make it so ragdoll can't collide with invisible HRP, but don't let HRP fall through map and be destroyed in process
  65. --character.HumanoidRootPart.Anchored = true
  66.  
  67. --Helps to fix constraint spasms
  68. recurse(character, function(_,v)
  69. if v:IsA("Attachment") then
  70. v.Axis = Vector3.new(0, 1, 0)
  71. v.SecondaryAxis = Vector3.new(0, 0, 1)
  72. v.Rotation = Vector3.new(0, 0, 0)
  73. end
  74. end)
  75.  
  76. --Re-attach hats
  77. for _,child in next,character:GetChildren() do
  78. if child:IsA("Accoutrement") then
  79. --Loop through all parts instead of only checking for one to be forwards-compatible in the event
  80. --ROBLOX implements multi-part accessories
  81. for _,part in next,child:GetChildren() do
  82. if part:IsA("BasePart") then
  83. local attachment1 = part:FindFirstChildOfClass("Attachment")
  84. local attachment0 = getAttachment0(character, attachment1.Name)
  85. if attachment0 and attachment1 then
  86. --Shouldn't use constraints for this, but have to because of a ROBLOX idiosyncrasy where
  87. --joints connecting a character are perpetually deleted while the character is dead
  88. local constraint = Instance.new("HingeConstraint")
  89. constraint.Attachment0 = attachment0
  90. constraint.Attachment1 = attachment1
  91. constraint.LimitsEnabled = true
  92. constraint.UpperAngle = 0 --Simulate weld by making it difficult for constraint to move
  93. constraint.LowerAngle = 0
  94. constraint.Parent = character
  95. end
  96. end
  97. end
  98. end
  99. end
  100.  
  101. ragdollJoint(character, character.LowerTorso, character.UpperTorso, "Waist", "BallSocket", {
  102. {"LimitsEnabled",true};
  103. {"UpperAngle",5};
  104. })
  105. ragdollJoint(character, character.UpperTorso, character.Head, "Neck", "BallSocket", {
  106. {"LimitsEnabled",true};
  107. {"UpperAngle",15};
  108. })
  109.  
  110. local handProperties = {
  111. {"LimitsEnabled", true};
  112. {"UpperAngle",0};
  113. {"LowerAngle",0};
  114. }
  115. ragdollJoint(character, character.LeftLowerArm, character.LeftHand, "LeftWrist", "Hinge", handProperties)
  116. ragdollJoint(character, character.RightLowerArm, character.RightHand, "RightWrist", "Hinge", handProperties)
  117.  
  118. local shinProperties = {
  119. {"LimitsEnabled", true};
  120. {"UpperAngle", 0};
  121. {"LowerAngle", -75};
  122. }
  123. ragdollJoint(character, character.LeftUpperLeg, character.LeftLowerLeg, "LeftKnee", "Hinge", shinProperties)
  124. ragdollJoint(character, character.RightUpperLeg, character.RightLowerLeg, "RightKnee", "Hinge", shinProperties)
  125.  
  126. local footProperties = {
  127. {"LimitsEnabled", true};
  128. {"UpperAngle", 15};
  129. {"LowerAngle", -45};
  130. }
  131. ragdollJoint(character, character.LeftLowerLeg, character.LeftFoot, "LeftAnkle", "Hinge", footProperties)
  132. ragdollJoint(character, character.RightLowerLeg, character.RightFoot, "RightAnkle", "Hinge", footProperties)
  133.  
  134. --TODO fix ability for socket to turn backwards whenn ConeConstraints are shipped
  135. ragdollJoint(character, character.UpperTorso, character.LeftUpperArm, "LeftShoulder", "BallSocket")
  136. ragdollJoint(character, character.LeftUpperArm, character.LeftLowerArm, "LeftElbow", "BallSocket")
  137. ragdollJoint(character, character.UpperTorso, character.RightUpperArm, "RightShoulder", "BallSocket")
  138. ragdollJoint(character, character.RightUpperArm, character.RightLowerArm, "RightElbow", "BallSocket")
  139. ragdollJoint(character, character.LowerTorso, character.LeftUpperLeg, "LeftHip", "BallSocket")
  140. ragdollJoint(character, character.LowerTorso, character.RightUpperLeg, "RightHip", "BallSocket")
  141. ragdollJoint(character, character.LowerTorso, character.HumanoidRootPart, "Root", "Hinge")
  142. character.Humanoid:ChangeState(Enum.HumanoidStateType.Physics)
  143. end
  144.  
  145. function unragdoll(character)
  146. --character.HumanoidRootPart.WeldConstraint:Destroy()
  147. for i,v in pairs(welds) do
  148. print("Enabing welds")
  149. v.w.Parent = v.p
  150. end
  151.  
  152. for i,constraint in pairs(constraints) do
  153. constraint:Destroy()
  154. end
  155.  
  156. local camera = workspace.CurrentCamera
  157. camera.CameraSubject = character.Humanoid
  158. end
  159.  
  160.  
  161. game.ReplicatedStorage.Ragdoll.OnServerEvent:Connect(function(player, on)
  162. player.Character.UpperTorso:SetNetworkOwner(player)
  163. if(on) then
  164. ragdoll(player.Character)
  165. else
  166. unragdoll(player.Character)
  167. end
  168. end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement