Question about EgoMoose's ragdoll module

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

2 Likes

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:

1 Like