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