I need help with ragdolling my NPCS.
local ragdollSounds = {7446609976,7446609932,7446607140,7446607037,7446606796,7446606976,7446606925,7446610013}
local EoAfunc = require(game.ServerScriptService.Scripts.ModuleScripts.EaseofAccessFunc)
local hum = script.Parent
local timeLength = script.TimeLength.Value
function createRagdoll(char,timeLength)
if char:FindFirstChild("RagdollTag") or char:GetAttribute("CantBeStunned") then script:Destroy() return end
local ragdollTag = Instance.new("StringValue")
local hrp = char:WaitForChild("HumanoidRootPart")
ragdollTag.Name = "RagdollTag"
ragdollTag.Parent = char
if not char.HumanoidRootPart.Anchored then
char.HumanoidRootPart:SetNetworkOwner(nil)
end
local plr = game.Players:GetPlayerFromCharacter(char)
if char:FindFirstChild("Humanoid") and char.Humanoid.Health > 0 then
if plr then
hrp:SetNetworkOwner(nil)
task.wait()
stateEvent:FireClient(plr,"Physics")
else
hrp:SetNetworkOwner(nil)
task.wait()
char.Humanoid:ChangeState(Enum.HumanoidStateType.Physics)
char.Humanoid:SetStateEnabled(Enum.HumanoidStateType.Running,false)
char.Humanoid:SetStateEnabled(Enum.HumanoidStateType.GettingUp,false)
local config = char:FindFirstChild("NPCConfig",true)
if config then
config = require(config)
config.NPC.CanAttack = false
end
end
end
local debounce = false
local connection = char.Humanoid.RootPart.Touched:Connect(function()
if debounce then return end
debounce = true
EoAfunc.createNewSound(ragdollSounds[math.random(1,#ragdollSounds)],0.5,math.random(8,12)/10,char.Humanoid.RootPart)
task.wait(.1)
debounce = false
end)
for index,joint in pairs(char:GetDescendants()) do
if joint:IsA("Motor6D") then
local socket = Instance.new("BallSocketConstraint")
local a1 = Instance.new("Attachment")
local a2 = Instance.new("Attachment")
a1.Name = "a1"
a2.Name = "a2"
socket.Name = "socket"
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
end
end
for i,v in pairs(char:GetChildren()) do
if v:IsA("BasePart") and v.Name ~= "Head" and v.Name ~= "HumanoidRootPart" then
local part = Instance.new("Part")
part.CanQuery = false
part.Parent = char
part.CFrame = v.CFrame
part.CanCollide = true
part.Size = v.Size *0.75
part.Name = "cPart"
local weldcons = Instance.new('WeldConstraint')
weldcons.Parent = part
weldcons.Part0 = part
weldcons.Part1 = v
part.Transparency = 1
end
end
task.wait(timeLength)
ragdollTag:Destroy()
connection:Disconnect()
if char:FindFirstChild("Humanoid") and char.Humanoid.Health > 0 then
if plr then
hrp:SetNetworkOwnershipAuto()
stateEvent:FireClient(plr,"GettingUp")
else
hrp:SetNetworkOwner(nil)
task.wait()
char.Humanoid:ChangeState(Enum.HumanoidStateType.GettingUp)
char.Humanoid:SetStateEnabled(Enum.HumanoidStateType.Running,true)
char.Humanoid:SetStateEnabled(Enum.HumanoidStateType.GettingUp,true)
local config = char:FindFirstChild("NPCConfig",true)
if config then
config = require(config)
config.NPC.CanAttack = true
end
end
end
if not char.HumanoidRootPart.Anchored then
char.HumanoidRootPart:SetNetworkOwnershipAuto()
end
for _,joint in pairs(char:GetDescendants()) do
if joint:IsA("Motor6D") then
joint.Enabled = true
end
end
for _,badjoint in pairs(char:GetDescendants()) do
if badjoint.Name == "a1" or badjoint.Name == "a2" or badjoint.Name == "socket" then
badjoint:Destroy()
end
end
for _,collisionPart in pairs(char:GetChildren()) do
if collisionPart.Name == "cPart" then
collisionPart:Destroy()
end
end
end
createRagdoll(hum:FindFirstAncestorOfClass("Model"),timeLength)
This is the code thats giving trouble. I tried disabling states and setting their rootparts to nil, however NPCS still dont ragdoll correctly half of the time, They may ragdoll, but not get back up, and vice versa.