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