Guest User

Ragdoll Code

a guest
May 14th, 2021
14
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 10.74 KB | None | 0 0
  1. -- // SERVICES
  2. local Players = game:GetService('Players')
  3. local Debris = game:GetService('Debris')
  4.  
  5. -- // VARIABLES
  6. local Character = script.Parent
  7. local Player = Players:GetPlayerFromCharacter(Character)
  8. if not Player:IsA('Player') then print("Something went wrong, this isn't a player? Location: ".. script:GetFullName()) script:Destroy() return end
  9. local Humanoid = Character:WaitForChild('Humanoid',10)
  10. if not Humanoid then print("Something went wrong, cannot find Humanoid? Location: ".. script:GetFullName()) script:Destroy() return end
  11. local UpperTorso = Character:WaitForChild('UpperTorso',10)
  12. if not UpperTorso then print("Something went wrong, cannot find the UpperTorso? Location: ".. script:GetFullName()) script:Destroy() return end
  13. local HumanoidRootPart = Character:WaitForChild('HumanoidRootPart',10)
  14. if not HumanoidRootPart then print("Something went wrong, cannot find the HumanoidRootPart? Location: ".. script:GetFullName()) script:Destroy() return end
  15.  
  16. local SendEffect = script:WaitForChild('SendEffect')
  17. local Carried = false
  18. local CarryOwner = nil
  19. local CarryWeld = nil
  20. local WakeUp = (script.WakeUpAnim.Value and Humanoid:LoadAnimation(script.WakeUp)) or nil
  21. local RDWELDS,MotorList,GlueList,ColliderList = {},{},{},{}
  22.  
  23. function getNearestPlayer( position )
  24.     local Players = game.Players:GetChildren();
  25.     local dist = math.huge;
  26.     local ret = nil;
  27.     for i=1,#Players do
  28.         local Player = Players[i];
  29.         local Character = Player.Character;
  30.         if ( Character ~= nil ) then
  31.             local Root = Character:FindFirstChild("HumanoidRootPart");
  32.             if ( Root ~= nil ) then
  33.                 local t = (position - Root.Position).magnitude;
  34.                 if ( t < dist ) then
  35.                     dist = t;
  36.                     ret = Player;
  37.                 end
  38.             end
  39.         end
  40.     end
  41.    
  42.     return ret;
  43. end
  44.  
  45. local RootLimbData = {
  46.     {
  47.         ["WeldTo"]          = "LowerTorso",
  48.         ["WeldRoot"]        = "HumanoidRootPart",
  49.         ["AttachmentName"]  = "Root",
  50.         ["NeedsCollider"]   = false,
  51.         ["UpperAngle"]      = 10
  52.     },
  53.     {
  54.         ["WeldTo"]          = "UpperTorso",
  55.         ["WeldRoot"]        = "LowerTorso",
  56.         ["AttachmentName"]  = "Waist",
  57.         ["ColliderOffset"]  = CFrame.new(0, 0.5, 0),
  58.         ["UpperAngle"]      = 0
  59.     },
  60.     {
  61.         ["WeldTo"]          = "Head",
  62.         ["WeldRoot"]        = "UpperTorso",
  63.         ["AttachmentName"]  = "Neck",
  64.         ["ColliderOffset"]  = CFrame.new(),
  65.         ["UpperAngle"]      = 20
  66.     },
  67.     {
  68.         ["WeldTo"]          = "LeftUpperLeg",
  69.         ["WeldRoot"]        = "LowerTorso",
  70.         ["AttachmentName"]  = "LeftHip",
  71.         ["ColliderOffset"]  = CFrame.new(0, -0.5, 0)
  72.     },
  73.     {
  74.         ["WeldTo"]          = "RightUpperLeg",
  75.         ["WeldRoot"]        = "LowerTorso",
  76.         ["AttachmentName"]  = "RightHip",
  77.         ["ColliderOffset"]  = CFrame.new(0, -0.5, 0)
  78.     },
  79.     {
  80.         ["WeldTo"]          = "RightLowerLeg",
  81.         ["WeldRoot"]        = "RightUpperLeg",
  82.         ["AttachmentName"]  = "RightKnee",
  83.         ["ColliderOffset"]  = CFrame.new(0, -0.5, 0)
  84.     },
  85.     {
  86.         ["WeldTo"]          = "LeftLowerLeg",
  87.         ["WeldRoot"]        = "LeftUpperLeg",
  88.         ["AttachmentName"]  = "LeftKnee",
  89.         ["ColliderOffset"]  = CFrame.new(-0.05, -0.5, 0)
  90.     },
  91.     {
  92.         ["WeldTo"]          = "RightUpperArm",
  93.         ["WeldRoot"]        = "UpperTorso",
  94.         ["AttachmentName"]  = "RightShoulder",
  95.         ["ColliderOffset"]  = CFrame.new(0.05, 0.45, 0.15),
  96.     },
  97.     {
  98.         ["WeldTo"]          = "LeftUpperArm",
  99.         ["WeldRoot"]        = "UpperTorso",
  100.         ["AttachmentName"]  = "LeftShoulder",
  101.         ["ColliderOffset"]  = CFrame.new(0, 0.45, 0.15),
  102.     },
  103.     {
  104.         ["WeldTo"]          = "LeftLowerArm",
  105.         ["WeldRoot"]        = "LeftUpperArm",
  106.         ["AttachmentName"]  = "LeftElbow",
  107.         ["ColliderOffset"]  = CFrame.new(0, 0.125, 0),
  108.         ["UpperAngle"]      = 10
  109.     },
  110.     {
  111.         ["WeldTo"]          = "RightLowerArm",
  112.         ["WeldRoot"]        = "RightUpperArm",
  113.         ["AttachmentName"]  = "RightElbow",
  114.         ["ColliderOffset"]  = CFrame.new(0, 0.125, 0),
  115.         ["UpperAngle"]      = 10
  116.     },
  117.     {
  118.         ["WeldTo"]          = "RightHand",
  119.         ["WeldRoot"]        = "RightLowerArm",
  120.         ["AttachmentName"]  = "RightWrist",
  121.         ["ColliderOffset"]  = CFrame.new(0, 0.125, 0),
  122.         ["UpperAngle"]      = 0
  123.     },
  124.     {
  125.         ["WeldTo"]          = "LeftHand",
  126.         ["WeldRoot"]        = "LeftLowerArm",
  127.         ["AttachmentName"]  = "LeftWrist",
  128.         ["ColliderOffset"]  = CFrame.new(0, 0.125, 0),
  129.         ["UpperAngle"]      = 0
  130.     },
  131.     {
  132.         ["WeldTo"]          = "LeftFoot",
  133.         ["WeldRoot"]        = "LeftLowerLeg",
  134.         ["AttachmentName"]  = "LeftAnkle",
  135.         ["NeedsCollider"]   = false,
  136.         ["UpperAngle"]      = 0
  137.     },
  138.     {
  139.         ["WeldTo"]          = "RightFoot",
  140.         ["WeldRoot"]        = "RightLowerLeg",
  141.         ["AttachmentName"]  = "RightAnkle",
  142.         ["NeedsCollider"]   = false,
  143.         ["UpperAngle"]      = 0
  144.     },
  145. }
  146.  
  147. local Weld = {
  148.     Welder = nil;
  149.     WeldBetween = function(self, Main, x)
  150.         local W = Instance.new("Weld")
  151.         W.Name = "Weld"
  152.         W.Part0 = Main
  153.         W.Part1 = x
  154.         local CJ = CFrame.new(Main.Position)
  155.         local C0 = Main.CFrame:inverse()*CJ
  156.         local C1 = x.CFrame:inverse()*CJ
  157.         W.C0 = C0
  158.         W.C1 = C1
  159.         W.Parent = Main
  160.         table.insert(RDWELDS,#RDWELDS+1,W) 
  161.     end;
  162.     WeldTG = function(self, targ, base, unanch, clld)
  163.         for _, prt  in pairs(targ:children()) do
  164.             if prt:IsA('BasePart') then
  165.                 self:WeldBetween(base,prt)
  166.                 prt.Anchored = unanch or false
  167.                 prt.CanCollide = clld or false
  168.                 if prt.Name == 'Barrel' or prt.Name == 'Handle' then prt.Transparency = 1 end
  169.             end
  170.         end
  171.     end;
  172.     Clear = function(self)
  173.         for _, prt  in pairs(RDWELDS) do
  174.             prt.Part1.CanCollide = true;
  175.             prt:Destroy()
  176.         end
  177.         RDWELDS = {}
  178.     end
  179. }
  180.  
  181. SendEffect.OnServerEvent:connect(function(p,action)
  182.     if action == "Carry" then
  183.         if Carried == false and script.Activate.Value then
  184.             if not UpperTorso then return end
  185.             Weld:WeldTG(Character, UpperTorso)
  186.             Carried = true;
  187.             CarryOwner = p.Character
  188.             p.Character.UpperTorso.Anchored = true
  189.             script.CarryOwner.Value = p.Character
  190.             CarryWeld = Instance.new('Weld', script)
  191.             CarryWeld.Part0 = p.Character.UpperTorso
  192.             CarryWeld.Part1 = UpperTorso
  193.             CarryWeld.C0 = CFrame.new(0,0,-2)*CFrame.Angles(math.rad(90),0,math.rad(90))
  194.             p.Character.UpperTorso.Anchored = false
  195.         end
  196.     elseif action == "Drop" then
  197.         Carried = false
  198.         pcall(function() CarryWeld:Destroy() CarryWeld = nil end)
  199.         Weld:Clear()
  200.         CarryOwner = nil
  201.         script.CarryOwner.Value = nil
  202.     elseif action == 'WakeUp' then
  203.         script.Activate.Value = false
  204.     end
  205. end)
  206.  
  207. function deactivate()
  208.     Carried = false
  209.     pcall(function() CarryWeld:Destroy() CarryWeld = nil end)
  210.     Weld:Clear()
  211.     CarryOwner = nil
  212.     script.CarryOwner.Value = nil
  213.     if WakeUp ~= nil then WakeUp:Play() end
  214.     SendEffect:FireClient(Player, "DeactivateAll")
  215.    
  216.     local posser = UpperTorso.CFrame;
  217.     if UpperTorso ~= nil then
  218.         UpperTorso:SetNetworkOwner(nil);
  219.         HumanoidRootPart.CFrame = CFrame.new(UpperTorso.CFrame.p + Vector3.new(0,3,0));
  220.         HumanoidRootPart.Anchored = true
  221.     end
  222.     for i=1,#MotorList do
  223.         local MotorData = MotorList[i];
  224.         local PartTo = MotorData[1];
  225.         local Motor = MotorData[2];
  226.         Motor.Parent = PartTo;
  227.     end
  228.     for i=1,#GlueList do GlueList[i]:Destroy(); end
  229.     for i=1,#ColliderList do ColliderList[i]:Destroy(); end
  230.     Humanoid.PlatformStand = false;
  231.     HumanoidRootPart.Parent = Character;
  232.     MotorList,GlueList = {},{}
  233.     wait()
  234.     HumanoidRootPart.Anchored = false
  235. end
  236.  
  237. function activate()
  238.     if HumanoidRootPart == 'NF' then return end
  239.    
  240.     local Position,RagDollModel = HumanoidRootPart.Position, Character
  241.     SendEffect:FireClient(Player, "ActivateBlur")
  242.     Humanoid:UnequipTools()
  243.     Humanoid.PlatformStand = true;
  244.    
  245.     if Humanoid.Health <= 0 and script.ActivateOnDeath.CloneAndDestroy.Value then
  246.         HumanoidRootPart:Destroy(); HumanoidRootPart = 'NF' -- not found
  247.         Character.Archivable = true;
  248.         RagDollModel = Character:Clone();
  249.         RagDollModel.Name = "Ragdoll";
  250.        
  251.         for _,v in next, RagDollModel:GetDescendants() do
  252.             if v:IsA("Script") or v:IsA("LocalScript") then
  253.                 v:Destroy();
  254.             elseif v:IsA('Humanoid') then
  255.                 v.PlatformStand = true;    
  256.                 v.DisplayDistanceType = Enum.HumanoidDisplayDistanceType.None;
  257.                 v.HealthDisplayType = Enum.HumanoidHealthDisplayType.AlwaysOff;
  258.                 v.Health = 0;
  259.             end
  260.         end
  261.        
  262.         for _,v in next, Character:GetDescendants() do
  263.             if v:IsA("BasePart") or v:IsA("Accessory") then
  264.                 v:Destroy();
  265.             end
  266.         end
  267.        
  268.         Debris:AddItem(RagDollModel, script.ActivateOnDeath.CloneAndDestroy.Delay.Value);
  269.         RagDollModel.Parent = workspace;
  270.     end
  271.  
  272.     for i=1,#RootLimbData do
  273.         local limbData = RootLimbData[i];
  274.         local partName = limbData["WeldTo"];
  275.         local weldName = limbData["WeldRoot"];
  276.         local PartTo = RagDollModel:FindFirstChild(partName);
  277.         local WeldTo = RagDollModel:FindFirstChild(weldName);
  278.        
  279.         if ( PartTo ~= nil and WeldTo ~= nil ) then
  280.             if ( RagDollModel ~= nil ) then
  281.                 if script.ApplyRandomVelocity.Value then
  282.                     local scale = script.ApplyRandomVelocity.Force.Value;
  283.                     local vecX = (math.random()-math.random())*scale;
  284.                     local vecY = (math.random()-math.random())*scale;
  285.                     local vecZ = (math.random()-math.random())*scale;
  286.                     PartTo.Velocity = PartTo.Velocity + Vector3.new(vecX, vecY, vecZ);
  287.                 end
  288.                 PartTo.Parent = RagDollModel;
  289.             end
  290.            
  291.             local UpperAngle = limbData["UpperAngle"];
  292.             local Joint = Instance.new("HingeConstraint");
  293.             if ( (UpperAngle ~= nil and UpperAngle == 0) or (script.WeldHead.Value and partName == "Head") ) then
  294.                 Joint = Instance.new("HingeConstraint");
  295.                 Joint.UpperAngle = 0;
  296.                 Joint.LowerAngle = 0;
  297.             end
  298.             Joint.Name = limbData["AttachmentName"];
  299.             Joint.LimitsEnabled = true;
  300.             Joint.Attachment0 = PartTo:FindFirstChild(Joint.Name .. "RigAttachment");
  301.             Joint.Attachment1 = WeldTo:FindFirstChild(Joint.Name .. "RigAttachment");
  302.             Joint.Parent = PartTo;
  303.             GlueList[#GlueList+1] = Joint;
  304.             if ( UpperAngle ~= nil ) then
  305.                 Joint.UpperAngle = UpperAngle;
  306.             end
  307.            
  308.             local Motor = PartTo:FindFirstChildOfClass("Motor6D");
  309.             if ( Motor ~= nil ) then
  310.                 if ( Humanoid.Health <= 0 ) then
  311.                     Motor:Destroy();
  312.                 else
  313.                     MotorList[#MotorList+1] = { PartTo, Motor };
  314.                     Motor.Parent = nil;
  315.                 end
  316.             end
  317.            
  318.             local needsCollider = limbData["NeedsCollider"];
  319.             if ( needsCollider == nil ) then
  320.                 needsCollider = true;
  321.             end
  322.             if ( needsCollider ) then
  323.                 local B = Instance.new("Part");
  324.                 B.CanCollide = true;
  325.                 B.TopSurface = 0;
  326.                 B.BottomSurface = 0;
  327.                 B.formFactor = "Symmetric";
  328.                 B.Size = Vector3.new(0.7, 0.7, 0.7);
  329.                 B.Transparency = 1;
  330.                 B.BrickColor = BrickColor.Red();
  331.                 B.Parent = RagDollModel;
  332.                 local W = Instance.new("Weld");
  333.                 W.Part0 = PartTo;
  334.                 W.Part1 = B;
  335.                 W.C0 = limbData["ColliderOffset"];
  336.                 W.Parent = PartTo;
  337.                 ColliderList[#ColliderList+1] = B;
  338.             end
  339.         end
  340.     end
  341.    
  342.     if HumanoidRootPart ~= 'NF' then
  343.         HumanoidRootPart.Parent = nil;
  344.     end
  345.    
  346.     if script.GivePlayerPhysics.Value then
  347.         local PlayerPhysics = Player
  348.         if ( script.GivePlayerPhysics.ForceNearestPlayer.Value ) then
  349.             PlayerPhysics = getNearestPlayer( Position );
  350.         end
  351.         for _,v in next, Character:GetDescendants() do
  352.             if v:IsA("BasePart") then
  353.                 v:SetNetworkOwner(PlayerPhysics);
  354.             end
  355.         end
  356.     end
  357. end
  358.  
  359. Humanoid.Died:Connect(function()
  360.     if script.ActivateOnDeath.Value then
  361.         script.Activate.Value = true
  362.     end
  363. end);
Advertisement
Add Comment
Please, Sign In to add comment