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