· 5 years ago · Sep 16, 2020, 04:36 PM
1--| Variables |
2
3local ragdoll_Folder_Name = 'Instances | Ragdoll Module';
4
5local data_Bank_Dictionary = {
6 ['attachments'] = {};
7 ['motors_6D'] = {};
8};
9
10local debug_Mode = false;
11local module = {};
12
13--| Main Code |
14
15function module:un_Ragdoll(character)
16 if (character) then
17 local humanoid_Root_Part = character:FindFirstChild('HumanoidRootPart');
18 local ragdoll_Folder = character:FindFirstChild(ragdoll_Folder_Name);
19 local humanoid = character:FindFirstChild('Humanoid');
20
21 if (humanoid and humanoid_Root_Part and ragdoll_Folder) then
22 --: Iterate on the motors_6D table inside data_Bank_Dictionary | Is there to un ragdoll the character.
23
24 for motor_6D, part_0 in pairs(data_Bank_Dictionary['motors_6D']) do
25 data_Bank_Dictionary['motors_6D'][motor_6D] = nil;
26 motor_6D.Part0 = part_0;
27 end;
28
29 --: Iterate on the attachments table inside data_Bank_Dictionary | Is there to un ragdoll the character and destroy the attachments.
30
31 for _, attachment in ipairs(data_Bank_Dictionary['attachments']) do
32 data_Bank_Dictionary['attachments'][attachment] = nil;
33 attachment:Destroy();
34 end;
35
36 ragdoll_Folder:Destroy();
37
38 humanoid:ChangeState(Enum.HumanoidStateType.Running);
39 humanoid.PlatformStand = false;
40 humanoid.AutoRotate = true;
41 else
42 warn('Failed to un ragdoll. | Character does not have one of those: Humanoid, HumanoidRootPart! | Error from Ragdoll Module.');
43 end;
44 else
45 warn('Failed to un ragdoll. | Character does not exist! | Error from Ragdoll Module.')
46 end;
47end;
48
49function module:ragdoll(character, selected_Time)
50 if (character) then
51 local humanoid_Root_Part = character:FindFirstChild('HumanoidRootPart');
52 local ragdoll_Folder = character:FindFirstChild(ragdoll_Folder_Name);
53 local humanoid = character:FindFirstChild('Humanoid');
54
55 --: Check if character has humanoid, humanoid root part and not ragdoll folder | Is there to prevent errors and to prevent over ragdolling.
56
57 if (humanoid and humanoid_Root_Part and not ragdoll_Folder) then
58 --: Creates a folder | We are going to store Parts and ball socket constraints there. Easier to clean everything after.
59
60 ragdoll_Folder = Instance.new('Folder');
61
62 local start_Time_Taken;
63
64 if (debug_Mode) then
65 start_Time_Taken = os.clock();
66 end;
67
68 humanoid_Root_Part.Anchored = false;
69
70 humanoid:ChangeState(Enum.HumanoidStateType.Physics);
71 humanoid.PlatformStand = true;
72 humanoid.AutoRotate = false;
73
74 ragdoll_Folder.Name = ragdoll_Folder_Name;
75 ragdoll_Folder.Parent = character;
76
77 --: Ragdoll loop | Is there to ragdoll character.
78 for _, descendant in pairs(character:GetDescendants()) do
79 --: Check if Descendant is a descendant of HumanoidRootPart.
80
81 if (descendant:IsDescendantOf(humanoid_Root_Part)) then continue; end;
82
83 --: Checks if the descendant is a descendant of a accessory of the character | Is here to not ragdoll accessories and such.
84
85 local first_Ancestor_Accessory = descendant:FindFirstAncestorWhichIsA('Accessory');
86 if (first_Ancestor_Accessory) then
87 if (first_Ancestor_Accessory.Parent == character) then
88 continue;
89 end;
90 end;
91
92 if (descendant:IsA('BasePart') and descendant ~= humanoid_Root_Part) then
93 --: Creates hitboxes for collision | else character would not be able to collide.
94
95 local hitbox = Instance.new('Part');
96 local weld = Instance.new('Weld');
97
98 hitbox.Name = 'Hitbox | Ragdoll Module';
99 hitbox.Size = descendant.Size * (0.75);
100 hitbox.CanCollide = true;
101
102 if (debug_Mode) then hitbox.Transparency = (0.5); else hitbox.Transparency = (1); end;
103
104 weld.Part0 = descendant;
105 weld.Part1 = hitbox;
106
107 hitbox.Parent = ragdoll_Folder;
108 weld.Parent = hitbox;
109 elseif (descendant:IsA('Motor6D')) then
110 --: This is the principal part. | We will create everything here. This is what actually ragdolls your character.
111
112 local ball_Socket_Constraint = Instance.new('BallSocketConstraint');
113
114 local second_Attachment = Instance.new('Attachment');
115 local first_Attachment = Instance.new('Attachment');
116
117 ball_Socket_Constraint.Name = 'ball_Socket_Constraint | Ragdoll Module';
118 second_Attachment.Name = 'second_Attachment | Ragdoll Module';
119 first_Attachment.Name = 'first_Attachment | Ragdoll Module';
120
121 second_Attachment.CFrame = descendant.C1;
122 first_Attachment.CFrame = descendant.C0;
123
124 ball_Socket_Constraint.Attachment1 = second_Attachment;
125 ball_Socket_Constraint.Attachment0 = first_Attachment;
126
127 ball_Socket_Constraint.Parent = ragdoll_Folder;
128 second_Attachment.Parent = descendant.Part1;
129 first_Attachment.Parent = descendant.Part0;
130
131 --: Add the attachments to "attachments" table inside data_Bank_Dictionary | Is here to allow us un ragdoll after.
132
133 table.insert(data_Bank_Dictionary['attachments'], second_Attachment);
134 table.insert(data_Bank_Dictionary['attachments'], first_Attachment);
135
136 --: Add the motors6D to "motors_6D" table inside data_Bank_Dictionary | Is here to allow us un ragdoll after.
137
138 data_Bank_Dictionary['motors_6D'][descendant] = descendant.Part0;
139
140 descendant.Part0 = nil;
141 end;
142 end;
143
144 --: Checks if selected_Time exists. | Is used to un ragdoll the character after X time.
145
146 if (selected_Time) then
147 wait(selected_Time);
148
149 module:un_Ragdoll(character);
150 end;
151
152 if (start_Time_Taken) then
153 print('Time taken to ragdoll and unragdoll: '..os.clock() - start_Time_Taken..' | Ragdoll Module.');
154 end;
155 else
156 warn('Failed to ragdoll. | Could not find humanoid! | Error from Ragdoll Module.');
157 end;
158 else
159 warn('Failed to ragdoll. | Character does not exist! | Error from Ragdoll Module.');
160 end;
161end;
162
163--: Returns the module table | Is there to make us be able to call the functions.
164return module;