How to go about making a ragdoll NPC?

Hello Developers,

I was recently working on a meme-fighting game with ragdolls. The thing is that every time I want to test on a another player (studio local servers), it takes a whole lot of time. So I wanted to create an NPC which had the same capabilities. I’m currently using an Open Source resource for my ragdoll.

IMPORTANT: Ragdoll does work on other players.

Here’s the scripts:

ServerScriptService:

--[[
	Put this script in ServerScriptService.
]]--

local module = require(script.ModuleScript)

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(char)
		-- Setup ragdoll
		local clones = {}
		for i, v in pairs(script.RagdollParts:GetChildren()) do
			clones[v.Name] = v:Clone()
		end

		for i, v in pairs(clones) do
			if v:IsA("Attachment") then
				v.Parent = char[v:GetAttribute("Parent")]

			elseif v:IsA("BallSocketConstraint") then
				v.Parent = char.Torso
				v.Attachment0 = clones[v:GetAttribute("0")]
				v.Attachment1 = clones[v:GetAttribute("1")]

			else
				v.Part0 = char.HumanoidRootPart
				v.Part1 = char.Torso
				v.Parent = char.HumanoidRootPart
			end
		end
		
		-- Ragdoll trigger
		local trigger = Instance.new("BoolValue")
		trigger.Name = "RagdollTrigger"
		trigger.Parent = char
		
		trigger.Changed:Connect(function(bool)
			if bool then
				module:Ragdoll(char)
			else
				module:Unragdoll(char)
			end
		end)
	end)
end)

Module Scripts inside the above script:

local module = {}

local PhysicsService = game:GetService("PhysicsService")

function module:Ragdoll(char)
	local hrp = char.HumanoidRootPart
	local humanoid = char.Humanoid
	local weld = hrp.RagdollWeld

	weld.Enabled = true
	hrp.CanCollide = false
	humanoid.AutoRotate = false
	humanoid:ChangeState(Enum.HumanoidStateType.Physics)
	for i, v in pairs(char:GetDescendants()) do
		if v:IsA("BasePart") then
			PhysicsService:SetPartCollisionGroup(v, "Ragdoll")

		elseif v:IsA("Motor6D") then
			v.Enabled = false

		elseif v:IsA("BallSocketConstraint") then
			v.Enabled = true
		end
	end
end

function module:Unragdoll(char)
	local hrp = char.HumanoidRootPart
	local humanoid = char.Humanoid
	local weld = hrp.RagdollWeld

	weld.Enabled = false
	hrp.CanCollide = true
	humanoid.AutoRotate = true
	humanoid:ChangeState(Enum.HumanoidStateType.GettingUp)
	for i, v in pairs(char:GetDescendants()) do
		if v:IsA("BasePart") then
			PhysicsService:SetPartCollisionGroup(v, "Default")

		elseif v:IsA("Motor6D") then
			v.Enabled = true

		elseif v:IsA("BallSocketConstraint") then
			v.Enabled = false
		end
	end
end

return module

Localscript inside NPC:



local char = script.Parent
local ragollTrigger = char:WaitForChild("RagdollTrigger")
local humanoid = char:WaitForChild("Humanoid")

-- Makes hands interact with head better
local head = char:WaitForChild("Head")
head.Shape = Enum.PartType.Ball
head.Size = Vector3.new(1,1,1)

ragollTrigger.Changed:Connect(function(bool)
	if bool then
		humanoid:ChangeState(Enum.HumanoidStateType.Physics)
	else
		humanoid:ChangeState(Enum.HumanoidStateType.GettingUp)
	end
end)

Any help is GREATLY appreciated,
OceanTubez

1 Like

I don’t really get what your trying to say here, are you saying that only for the NPC the ragdoll does not work?

So what I’m trying to say here is that the ragdoll works on other players, but not NPCs.

Yes, only for the NPC the ragdoll doesn’t work.

Nothing in the script provided under that would work because your not placing it inside the npc but rather if I am correct the game is waiting for a player.

I changed the code and it’s location, Sorry for the miscommunication.

I’ve figured out a way by contacting the module owner himself.
Thanks for replying to this topic!

How did you fix this? I need to know how to fix it too

Uh, You need to first clarify what module you are using.

Are using this module?

If you are, just insert this script inside the NPC:

require(game.ServerScriptService.R6Ragdoll.ModuleScript):Setup(script.Parent)

Make sure you have the latest version, Here’s the link to it:
https://www.roblox.com/library/9292864894/R6-Ragdoll-Plug-and-Play?ViewInBrowser=true&CreatorId=97342104&SearchId=2AE4D028-1AC5-43D5-980E-6616788D5AC0&Category=Model&Position=1&SearchKeyword=&CreatorType=User&SortType=Relevance

4 Likes

Thank you, I got it to work after a while.

1 Like

When the npc dies and the ragdoll happens, the npc just flies away?