Ragdoll script kills players with high latency

I made a script that ragdolls the player that it is put in. It does this by creating new attachments at every player joint and connecting them with constraints that are in the script. When a player has a slightly high latency, the player sometimes just dies instead of ragdolling. Why does this happen and how can i prevent it? This is the code (sorry for the length, it has to go through all 15 joints of a player):

local char = script.Parent
local toEnable = {}
local toDelete = {}

if char:FindFirstChild('Humanoid') and char.Humanoid.Health > 0 then
	if char:FindFirstChild('LowerTorso') and char:FindFirstChild('UpperTorso') then
		local constraint = script.WaistRig
		constraint.Attachment0 = char.LowerTorso.WaistRigAttachment
		constraint.Attachment1 = char.UpperTorso.WaistRigAttachment
		constraint.Enabled = true
		char.UpperTorso.Waist.Enabled = false
		table.insert(toEnable,char.UpperTorso.Waist)
		
		local noCollision = Instance.new('NoCollisionConstraint')
		noCollision.Part0 = char.LowerTorso
		noCollision.Part1 = char.UpperTorso
		noCollision.Parent = char.LowerTorso
		table.insert(toDelete,noCollision)
		
		if char:FindFirstChild('Head') and char:FindFirstChild('UpperTorso') then
			local attach0 = Instance.new('Attachment')
			attach0.CFrame = CFrame.new(char.UpperTorso.NeckRigAttachment.Position) * CFrame.Angles(0,0,math.rad(90))
			attach0.Parent = char.UpperTorso
			table.insert(toDelete,attach0)

			local attach1 = Instance.new('Attachment')
			attach1.CFrame = CFrame.new(char.Head.NeckRigAttachment.Position) * CFrame.Angles(0,0,math.rad(90))
			attach1.Parent = char.Head
			table.insert(toDelete,attach1)

			local constraint = script.NeckRig
			constraint.Attachment0 = attach0
			constraint.Attachment1 = attach1
			constraint.Enabled = true
			char.Head.Neck.Enabled = false
			table.insert(toEnable,char.Head.Neck)
			
			local noCollision = Instance.new('NoCollisionConstraint')
			noCollision.Part0 = char.Head
			noCollision.Part1 = char.UpperTorso
			noCollision.Parent = char.Head
			table.insert(toDelete,noCollision)
		end
	end
	local side = {'Left','Right'}
	for i=1,2 do
		inverter = 1
		if i == 1 then
			inverter = -1
		end
		if char:FindFirstChild(side[i]..'UpperArm') and char:FindFirstChild('UpperTorso') then
			local attach0 = Instance.new('Attachment')
			if i==1 then
				attach0.CFrame = CFrame.new(char.UpperTorso[side[i]..'ShoulderRigAttachment'].Position) * CFrame.Angles(0,math.rad(180),0)
			else
				attach0.CFrame = CFrame.new(char.UpperTorso[side[i]..'ShoulderRigAttachment'].Position)
			end
			attach0.Parent = char.UpperTorso
			table.insert(toDelete,attach0)

			local attach1 = Instance.new('Attachment')
			if i == 1 then
				attach1.CFrame = CFrame.new(char[side[i]..'UpperArm'][side[i]..'ShoulderRigAttachment'].Position) * CFrame.Angles(0,math.rad(180),0)
			else
				attach1.CFrame = CFrame.new(char[side[i]..'UpperArm'][side[i]..'ShoulderRigAttachment'].Position)
			end
			attach1.Parent = char[side[i]..'UpperArm']
			table.insert(toDelete,attach1)
			
			local constraint = script[side[i]..'ShoulderRig']
			constraint.Attachment0 = attach0
			constraint.Attachment1 = attach1
			constraint.Enabled = true
			char[side[i]..'UpperArm'][side[i]..'Shoulder'].Enabled = false
			table.insert(toEnable,char[side[i]..'UpperArm'][side[i]..'Shoulder'])
			
			local noCollision = Instance.new('NoCollisionConstraint')
			noCollision.Part0 = char[side[i]..'UpperArm']
			noCollision.Part1 = char.UpperTorso
			noCollision.Parent = char.UpperTorso
			table.insert(toDelete,noCollision)

			if char:FindFirstChild(side[i]..'UpperArm') and char:FindFirstChild(side[i]..'LowerArm') then
				local attach0 = Instance.new('Attachment')
				attach0.CFrame = CFrame.new(char[side[i]..'UpperArm'][side[i]..'ElbowRigAttachment'].Position) * CFrame.Angles(0,math.rad(180),0)
				attach0.Parent = char[side[i]..'UpperArm']
				table.insert(toDelete,attach0)

				local attach1 = Instance.new('Attachment')
				attach1.CFrame = CFrame.new(char[side[i]..'LowerArm'][side[i]..'ElbowRigAttachment'].Position) * CFrame.Angles(0,math.rad(180),0)
				attach1.Parent = char[side[i]..'LowerArm']
				table.insert(toDelete,attach1)

				local constraint = script[side[i]..'ElbowRig']
				constraint.Attachment0 = attach0
				constraint.Attachment1 = attach1
				constraint.Enabled = true
				char[side[i]..'LowerArm'][side[i]..'Elbow'].Enabled = false
				table.insert(toEnable,char[side[i]..'LowerArm'][side[i]..'Elbow'])
				
				local noCollision = Instance.new('NoCollisionConstraint')
				noCollision.Part0 = char[side[i]..'UpperArm']
				noCollision.Part1 = char[side[i]..'LowerArm']
				noCollision.Parent = char[side[i]..'UpperArm']
				table.insert(toDelete,noCollision)

				if char:FindFirstChild(side[i]..'LowerArm') and char:FindFirstChild(side[i]..'Hand') then		
					local attach0 = Instance.new('Attachment')
					attach0.CFrame = CFrame.new(char[side[i]..'LowerArm'][side[i]..'WristRigAttachment'].Position) * CFrame.Angles(0,0,math.rad(90*inverter))
					attach0.Parent = char[side[i]..'LowerArm']
					table.insert(toDelete,attach0)

					local attach1 = Instance.new('Attachment')
					attach1.CFrame = CFrame.new(char[side[i]..'Hand'][side[i]..'WristRigAttachment'].Position)  * CFrame.Angles(0,0,math.rad(90*inverter))
					attach1.Parent = char[side[i]..'Hand']
					table.insert(toDelete,attach1)

					local constraint = script[side[i]..'WristRig']
					constraint.Attachment0 = attach0
					constraint.Attachment1 = attach1
					constraint.Enabled = true
					char[side[i]..'Hand'][side[i]..'Wrist'].Enabled = false
					table.insert(toEnable,char[side[i]..'Hand'][side[i]..'Wrist'])
					
					local noCollision = Instance.new('NoCollisionConstraint')
					noCollision.Part0 = char[side[i]..'Hand']
					noCollision.Part1 = char[side[i]..'LowerArm']
					noCollision.Parent = char[side[i]..'Hand']
					table.insert(toDelete,noCollision)
				end
			end
		end
		
		if char:FindFirstChild(side[i]..'UpperLeg') and char:FindFirstChild('LowerTorso') then
			local attach0 = Instance.new('Attachment')
			attach0.CFrame = CFrame.new(char.LowerTorso[side[i]..'HipRigAttachment'].Position) * CFrame.Angles(0,0,math.rad(-90))
			attach0.Parent = char.LowerTorso
			table.insert(toDelete,attach0)
			
			local attach1 = Instance.new('Attachment')
			attach1.CFrame = CFrame.new(char[side[i]..'UpperLeg'][side[i]..'HipRigAttachment'].Position) * CFrame.Angles(0,0,math.rad(-90))
			attach1.Parent = char[side[i]..'UpperLeg']
			table.insert(toDelete,attach1)
			
			local constraint = script[side[i]..'HipRig']
			constraint.Attachment0 = attach0
			constraint.Attachment1 = attach1
			constraint.Enabled = true
			char[side[i]..'UpperLeg'][side[i]..'Hip'].Enabled = false
			table.insert(toEnable,char[side[i]..'UpperLeg'][side[i]..'Hip'])
			
			local noCollision = Instance.new('NoCollisionConstraint')
			noCollision.Part0 = char[side[i]..'UpperLeg']
			noCollision.Part1 = char.LowerTorso
			noCollision.Parent = char[side[i]..'UpperLeg']
			table.insert(toDelete,noCollision)
			
			if char:FindFirstChild(side[i]..'LowerLeg') and char:FindFirstChild(side[i]..'UpperLeg') then
				local attach0 = Instance.new('Attachment')
				attach0.CFrame = CFrame.new(char[side[i]..'UpperLeg'][side[i]..'KneeRigAttachment'].Position)
				attach0.Parent = char[side[i]..'UpperLeg']
				table.insert(toDelete,attach0)

				local attach1 = Instance.new('Attachment')
				attach1.CFrame = CFrame.new(char[side[i]..'LowerLeg'][side[i]..'KneeRigAttachment'].Position)
				attach1.Parent = char[side[i]..'LowerLeg']
				table.insert(toDelete,attach1)

				local constraint = script[side[i]..'KneeRig']
				constraint.Attachment0 = attach0
				constraint.Attachment1 = attach1
				constraint.Enabled = true
				char[side[i]..'LowerLeg'][side[i]..'Knee'].Enabled = false
				table.insert(toEnable,char[side[i]..'LowerLeg'][side[i]..'Knee'])
				
				local noCollision = Instance.new('NoCollisionConstraint')
				noCollision.Part0 = char[side[i]..'UpperLeg']
				noCollision.Part1 = char[side[i]..'LowerLeg']
				noCollision.Parent = char[side[i]..'UpperLeg']
				table.insert(toDelete,noCollision)
				
				if char:FindFirstChild(side[i]..'LowerLeg') and char:FindFirstChild(side[i]..'Foot') then
					local attach0 = Instance.new('Attachment')
					attach0.CFrame = CFrame.new(char[side[i]..'LowerLeg'][side[i]..'AnkleRigAttachment'].Position) * CFrame.Angles(0,0,math.rad(-90))
					attach0.Parent = char[side[i]..'LowerLeg']
					table.insert(toDelete,attach0)

					local attach1 = Instance.new('Attachment')
					attach1.CFrame = CFrame.new(char[side[i]..'Foot'][side[i]..'AnkleRigAttachment'].Position) * CFrame.Angles(0,0,math.rad(-90))
					attach1.Parent = char[side[i]..'Foot']
					table.insert(toDelete,attach1)

					local constraint = script[side[i]..'AnkleRig']
					constraint.Attachment0 = attach0
					constraint.Attachment1 = attach1
					constraint.Enabled = true
					char[side[i]..'Foot'][side[i]..'Ankle'].Enabled = false
					table.insert(toEnable,char[side[i]..'Foot'][side[i]..'Ankle'])
					
					local noCollision = Instance.new('NoCollisionConstraint')
					noCollision.Part0 = char[side[i]..'LowerLeg']
					noCollision.Part1 = char[side[i]..'Foot']
					noCollision.Parent = char[side[i]..'Foot']
					table.insert(toDelete,noCollision)
				end
			end
		end
	end
	char.Humanoid.Died:Connect(function()
		for _,v in pairs(script:GetChildren()) do
			if not v:IsA('BindableEvent') then
				v:Destroy()
			end
		end
	end)
	
	char.Humanoid.PlatformStand = true
	task.wait(.2)
	for i,v in pairs(char:GetChildren()) do
		if v:IsA('BasePart') and v.Name ~= 'HumanoidRootPart' then
			v.CanCollide = true
		end
	end
	if char:FindFirstChild('HumanoidRootPart') then
		char.HumanoidRootPart.CanCollide = false
	end
end
2 Likes

Are you running this script on client side?

No it is on the server side. would client side be better?

I though it might be on the Client Side since you mentioned player Latency

Asides, is there any output?

Also why you never used the toDelete table if you made it?

I only posted the function that creates the ragdoll. theres another function that un-ragdolls a player that uses that table.

There is no output. I just notice that when I want to make 10-15 players ragdoll at the same time they lag for a bit and then die instead of ragdoll. But it also sometimes happens when just one player ragdolls

Interesting, can you post the Un-Ragdolling function aswell?

1 Like

That is this part

script.Disable.Event:Connect(function()
	for _,v in pairs(toEnable) do
		v.Enabled = true
	end
	for _,v in pairs(script:GetChildren()) do
		if not v:IsA('BindableEvent') then
			v.Enabled = false
			v.Attachment0 = nil
			v.Attachment1 = nil
		end	
	end
	for _,v in pairs(toDelete) do
		v:Destroy()
	end
	if not char:GetAttribute('InSpace') then
		char.Humanoid.PlatformStand = false
	end
	script:Destroy()
end)

if script:GetAttribute('AutoDisable') > 0.1 then
	task.wait(script:GetAttribute('AutoDisable'))
	script.Disable:Fire()
end

It gets triggered by a bindable event, but it is not the cause of this problem

For some reason when you don’t disable the Neck Motor6D here:

			char.Head.Neck.Enabled = false

it partially works

what do you mean it partially works? The problem with the script is that it works fine in a clean studio environment, but when I test it in-game players randomly die

did you even test my recommendation?

You have to turn RequiresNeck off on the players humanoid. And make sure you do it before disabling the neck motor6d

1 Like

A pretty funny hack that I came up with is just creating a RopeConstraint that connects the Head to the UpperTorso. Disabling RequiresNeck in my case made it so resetting your character did not replicate to the server, so you were just forever stuck.

Oh wow that seems to have fixed it. And in the reset script i turn it on again after the joint is enabled

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.