How do you make ragdoll when hit?

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?

1 Like

Please bump because I really need help with this.

but when the bot or the player hits while they ragdoll they disappear and die.

I imagine this happens because the character is ragdolled twice, right? What you could do is using a BoolValue to recognize if a character is already ragdolled, and if they are, they should not get ragdolled and will just create another BoolValue in the character to indicate that they should keep ragdolled. To unragdoll them, check whenever a child of the character is removed from them that has the name of the BoolValue that indicates the ragdoll through the .ChildRemoved event, then if there is another BoolValue of the same in the character and, if not, then unragdoll.

If this doesn’t fix the problem, I imagine it could be because the MaxForce of your BodyVelocity is math.huge, which I recommend using 100000 instead.

Also an aditional information which I’m not sure: I think that the FallingDown HumanoidStateType is a bit glitchy when it is to make ragdolls because it could make your character get flung. I recommend using the Physics HumanoidStateType instead and make the character unable to get the FallingDown HumanoidStateType at least while they’re ragdolled.

But i barely know how to code. Can you simpilfy the words?

oh no i accidently choose solution

I recommend watching this video to learn a bit about ragdolls. I also recommend you starting with simpler projects; if you barely know how to code, you shouldn’t be messing with ragdolls.

And also please read about the Community guidelines and about posts in Scripting Support.

Yea but all I need is to just remove the glove while they are ragdolled

What is “the glove”? Show images or videos about your problem. The main post also isn’t specific enough, I don’t even know what you exactly mean by “hits while they ragdoll they disappear and die.”, you need to show how this happens or give a better explanation of the situation.

I think it isn’t causing a problem anymore so uh, I am gonna put this to rest until another problem pops up.

Yea it still didn’t fix it.

Here is the proof: https://youtu.be/pcZRbQFjxZQ

It is at 0:53

actually i just give up on ragdoll

You have to force the Humanoid to get up after the ragdoll ends. Do that using the :SetStateEnabled() and :ChangeState() methods.

But I did use :ChangeState() in my script.

Use :SetStateEnabled([state], false) for the FallingDown and Ragdoll state too to be sure. Also check their WalkSpeed and JumpHeight after the ragdoll stops through the explorer to be sure their WalkSpeed and JumpHeight is being changed back, since in the video it really seems like the character is just completely stunned, unable to walk or jump.

Also don’t forget to use :SetStateEnabled([state], true) after they’re unragdolled.