I need help with scripting ragdoll for my game, but when the bot or the player hits while they ragdoll they disappear and die.
Here is my code:
local Tool = script.Parent.Parent
local Debris = game:GetService(“Debris”)
local Anim = script:WaitForChild(“SlapAnim”)
local AnimTrack
local hitChars = {}
local debounce = false
Tool.Activated:Connect(function()
if debounce then
return
end
debounce = true
local humanoid = Tool.Parent:FindFirstChild("Humanoid")
if not AnimTrack then
AnimTrack = humanoid:LoadAnimation(Anim)
end
AnimTrack:Play()
wait(0.57)
debounce = false
end)
Tool.Hitbox.Touched:Connect(function(hit)
if hitChars[hit.Parent] or not debounce then
return
end
if hit.Parent:FindFirstChild(“Humanoid”) and not hit.Parent:FindFirstChild(“Invincible”) then
local invincible = Instance.new(“BoolValue”, hit.Parent)
invincible.Name = “Invincible”
local eChar = hit.Parent
local plrChar = Tool.Parent
local eHumanRootPart = eChar:FindFirstChild(“HumanoidRootPart”)
local plrHumanRootPart = plrChar:FindFirstChild(“HumanoidRootPart”)
local EHumanoid = eChar:FindFirstChild(“Humanoid”)
local pHead = plrChar:FindFirstChild(“Head”)
local eHead = eChar:FindFirstChild(“Head”)
local s = script.Parent.Parent.Hand.Hardslapsound:Clone()
s:Play()
s.Parent = eHumanRootPart.Parent.Torso
s.PlaybackSpeed = 1
local storeJumpPower = EHumanoid.JumpHeight
local storeWalkSpeed = EHumanoid.WalkSpeed
if plrHumanRootPart and eHumanRootPart then
script.Disabled = true
EHumanoid:ChangeState'FallingDown'
for index,joint in pairs(hit.Parent:GetDescendants()) do
if joint:IsA("Motor6D") then
local socket = Instance.new("BallSocketConstraint")
local a1 = Instance.new("Attachment")
local a2 = Instance.new("Attachment")
a1.Parent = joint.Part0
a2.Parent = joint.Part1
socket.Parent = joint.Parent
socket.Attachment0 = a1
socket.Attachment1 = a2
a1.CFrame = joint.C0
a2.CFrame = joint.C1
socket.LimitsEnabled = true
socket.TwistLimitsEnabled = true
joint.Enabled = false
game:GetService("Debris"):AddItem(a1, 1.5)
game:GetService("Debris"):AddItem(a2, 1.5)
game:GetService("Debris"):AddItem(socket, 1.5)
end
end
EHumanoid.WalkSpeed = 0
EHumanoid.JumpHeight = 0
if EHumanoid then
local humanoidRootPart = eHumanRootPart
local x, y, z = hit.Position.X, hit.Position.Y, hit.Position.Z
local rotationX, rotationY, rotationZ = 0, 0, 0
humanoidRootPart.CFrame = CFrame.new(x, y, z) * CFrame.Angles(math.rad(rotationX), math.rad(rotationY), math.rad(rotationZ)) * CFrame.Angles(math.rad(90), 0, 0)
end
local force = Instance.new("BodyVelocity", eHumanRootPart)
force.MaxForce = Vector3.new(8, 8, 8) * math.huge
local direction = (eHumanRootPart.CFrame.Position - plrHumanRootPart.CFrame.Position).Unit
force.Velocity = (direction + Vector3.new(0 ,1, 0)).Unit * 25
local rotation = Instance.new("BodyAngularVelocity", eHumanRootPart)
rotation.AngularVelocity = Vector3.new(1, 1, 1) * math.pi * math.random(2, 5)
rotation.MaxTorque = Vector3.new(1, 1, 7.5) * math.huge
rotation.P = 5000
DebrisService:AddItem(force, 0.35)
DebrisServer:AddItem(rotation, 0.35)
eChar.Humanoid.Sit = false
local player = game.Players:GetPlayerFromCharacter(Tool.Parent)
player.leaderstats.Slaps.Value = player.leaderstats.Slaps.Value + 1
end
hitChars[hit.Parent] = true
wait(1.25)
invincible:Destroy()
hit.Parent.Humanoid:ChangeState'GettingUp'
EHumanoid.WalkSpeed = storeWalkSpeed
EHumanoid.JumpHeight = storeJumpPower
for index, joint in pairs(hit.Parent:GetDescendants()) do
if joint:IsA("Motor6D") then
joint.Enabled = true
script.Disabled = false
hitChars[hit.Parent] = nil
end
end
end
end)
Please help because I am not good at scripting, also this is my first time making a topic. Another question, how do you make the whole code fit into that box?