function setRagdollEnabled(humanoid, isEnabled)
-- print("ragdolling")
local ragdollConstraints = humanoid.Parent:FindFirstChild("RagdollConstraints")
for _,constraint in pairs(ragdollConstraints:GetChildren()) do
if constraint:IsA("Constraint") then
print(constraint:FindFirstChild("Attachment1"))
if constraint:FindFirstChild("Attachment1") then
print(constraint:FindFirstChild("Attachment1").Parent)
end
local rigidJoint = constraint.RigidJoint.Value
local expectedValue = (not isEnabled) and constraint.Attachment1.Parent or nil
if rigidJoint.Part1 ~= expectedValue then
rigidJoint.Part1 = expectedValue
end
end
end
end
When a limb of the playerās is deleted I get the error āattempt to index nil with āParentāā directed at the
line ālocal expectedValue = (not isEnabled) and constraint.Attachment1.Parent or nilā
So what I did was print the constraint.Attachment1 and contraint.Attachment1.Parent. Throughout every print it printed nil. So I thought if itās always printing nil then if I made this line "local expectedValue = (not isEnabled) and constraint.Attachment1.Parent or nil"
into ālocal expectedValue = (not isEnabled) and or nilā there shouldnāt be a difference? But afterwards the ragdoll was enabled as soon as I moved the character. Iām really not sure what the ālocal expectedValue = (not isEnabled) and constraint.Attachment1.Parent or nilā is doing, specifically the āand constraint.Attachment1.Parent or nilā so can anyone explain how this works?
local expectedValue = (not isEnabled) and constraint.Attachment1.Parent or nil
is the same as
local expectedValue
if not isEnabled then
expectedValue = constraint.Attachment1.Parent
else
expectedValue = nil
end
However, Iām not exactly sure what the code is doing. Maybe itās checking to make sure that the Attachment1.Parent is different from the rigidJoint.Part1? The code is a little repetitive, however, since expectedValue will default to false when isEnabled = true, so you could also try to do:
local expectedValue = not isEnabled and constraint.Attachment1.Parent
Ohh so what finally fixed it and the reason constraint:FindFirstChild(āAttachment1ā) kept printing nil was because Attachment1 is a property of the constraint and I kept thinking it was an actual attachment so I treated it as if it was a child of constraint. So now by doing
if not isEnabled and constraint.Attachment then
expectedValue = constraint.Attachment1.Parent
else
expectedValue = nil
end
It now works when limbs are removed. Thank you for your explanation of local expectedValue = (not isEnabled) and constraint.Attachment1.Parent or nil formatting to be what is above. Iām just curious is there somewhere I can look to learn the different formatting you can do in lua/roblox? Like how did you know (not isEnabled) meant an if statement?
I learned about logical operators without If statements randomly by finding some complex scripts in the ToolBox. You can learn them faster and better in the DevForum, however, such as in the Resources ā Community Tutorials category. Here are some threads I found: