Advertisement
TheTomatooo

Ragdoll Module Script

Sep 13th, 2023
6,232
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.86 KB | Source Code | 0 0
  1. local module = {}
  2. local Players = game:GetService("Players")
  3.  
  4. local PhysicsService = game:GetService("PhysicsService")
  5. local ragParts = script.Parent.RagdollParts:GetChildren()
  6.  
  7. function module:Setup(char: Model)
  8.     local humanoid = char:FindFirstChild("Humanoid")
  9.     assert(humanoid, "Can only set-up ragdoll on R6 humanoid rigs")
  10.     assert(humanoid.RigType == Enum.HumanoidRigType.R6, "Can only set-up ragdoll on R6 humanoid rigs")
  11.     assert(humanoid.RootPart ~= nil, "No RootPart was found in the provided rig")
  12.     assert(char:FindFirstChild("HumanoidRootPart"), "No HumanoidRootPart was found in the provided rig")
  13.  
  14.     for _, v: BasePart in ipairs(char:GetDescendants()) do
  15.         if v:IsA("BasePart") then
  16.             v:SetNetworkOwner(nil)
  17.         end
  18.     end
  19.  
  20.     -- Setup ragdoll
  21.     char.Head.Size = Vector3.new(1, 1, 1)
  22.     humanoid.BreakJointsOnDeath = false
  23.     humanoid.RequiresNeck = false
  24.  
  25.     local clones = {}
  26.     for _, v in ipairs(ragParts) do
  27.         clones[v.Name] = v:Clone()
  28.     end
  29.  
  30.     local folder1 = Instance.new("Folder")
  31.     folder1.Name = "RagdollConstraints"
  32.     for _, v in pairs(clones) do
  33.         if v:IsA("Attachment") then
  34.             v.Parent = char[v:GetAttribute("Parent")]
  35.         elseif v:IsA("BallSocketConstraint") then
  36.             v.Attachment0 = clones[v:GetAttribute("0")]
  37.             v.Attachment1 = clones[v:GetAttribute("1")]
  38.             v.Parent = folder1
  39.         end
  40.     end
  41.     folder1.Parent = char
  42.  
  43.     local folder2 = Instance.new("Folder")
  44.     folder2.Name = "Motors"
  45.     local value
  46.     for _, v in ipairs(char.Torso:GetChildren()) do
  47.         if v:IsA("Motor6D") then
  48.             value = Instance.new("ObjectValue")
  49.             value.Value = v
  50.             value.Parent = folder2
  51.         end
  52.     end
  53.     folder2.Parent = folder1
  54.  
  55.     -- Ragdoll trigger
  56.     local trigger = Instance.new("BoolValue")
  57.     trigger.Name = "RagdollTrigger"
  58.     trigger.Parent = char
  59.  
  60.     trigger.Changed:Connect(function(bool)
  61.         if bool then
  62.             module:Ragdoll(char)
  63.         else
  64.             module:Unragdoll(char)
  65.         end
  66.     end)
  67. end
  68.  
  69. function module:Ragdoll(char: Model)
  70.     char.Humanoid:ChangeState(Enum.HumanoidStateType.Physics)
  71.     char.Humanoid.AutoRotate = false
  72.    
  73.     char:SetAttribute("Ragdolled",true)
  74.    
  75.     for _, v in ipairs(char.RagdollConstraints.Motors:GetChildren()) do
  76.         v.Value.Enabled = false
  77.     end
  78.  
  79.     for _, v in ipairs(char:GetChildren()) do
  80.         if v:IsA("BasePart") then
  81.             --PhysicsService:SetPartCollisionGroup(v, "Ragdoll")
  82.             v.CollisionGroup = "Ragdoll"
  83.         end
  84.     end
  85.  
  86.     char.HumanoidRootPart:ApplyAngularImpulse(Vector3.new(-90, 0, 0))
  87. end
  88.  
  89. function module:Unragdoll(char: Model)
  90.     char.Humanoid:ChangeState(Enum.HumanoidStateType.GettingUp)
  91.    
  92.     char:SetAttribute("Ragdolled",false)
  93.    
  94.     for _, v in ipairs(char.RagdollConstraints.Motors:GetChildren()) do
  95.         v.Value.Enabled = true
  96.     end
  97.  
  98.     for _, v in ipairs(char:GetChildren()) do
  99.         if v:IsA("BasePart") then
  100.             --PhysicsService:SetPartCollisionGroup(v, "Default")
  101.             v.CollisionGroup = "Default"
  102.         end
  103.     end
  104.     char.Humanoid.AutoRotate = true
  105. end
  106.  
  107. return module
  108.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement