Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

GetDescendants() not working properly?

I'm trying to make a script that adds ball socket joints to a player for a ragdoll. I use GetDescendants() to get everything in the character, and then a for loop to sort through the Motor6D joints.

local character = script.Parent

local desc = character:GetDescendants()

for i=1,#desc do
	
	if desc[i]:IsA("Motor6D")then 
  
		local reference0 = desc[i].Parent.Parent:FindFirstChild(desc[i].Part0.Name):FindFirstChild(desc[i].Name.."RigAttachment")
		local reference1 = desc[i].Parent:FindFirstChild(desc[i].Name.."RigAttachment")
		
		local references = {reference0,reference1}
		
		for object=1,#references do
			local attachment = Instance.new("Attachment",references[object].Parent)
			attachment.Name = (desc[i].Name.."BallSocketAttachment")
			attachment.Orientation = references[object].Orientation
			attachment.Position = references[object].Position
			attachment.Axis = references[object].Axis
			attachment.SecondaryAxis = references[object].SecondaryAxis
			attachment.WorldAxis = references[object].WorldAxis
			attachment.WorldOrientation = references[object].WorldOrientation
			attachment.WorldPosition = references[object].WorldPosition
			attachment.WorldSecondaryAxis = references[object].WorldSecondaryAxis
			
		end
		
		local ballsocket = Instance.new("BallSocketConstraint",reference1.Parent)
		ballsocket.Name = (desc[i].Name.."BallSocketConstraint")
		ballsocket.Radius = 0.15
		ballsocket.Enabled = true
		ballsocket.Attachment0 = reference0.Parent:FindFirstChild(desc[i].Name.."BallSocketAttachment")
		ballsocket.Attachment1 = reference1.Parent:FindFirstChild(desc[i].Name.."BallSocketAttachment")
		
	end
end

Despite this, the only joint that Get Descandants() finds is the joint for the Head and no other joint. What makes this more frustrating is there is a ragdoll script that can be found which uses GetDescandants() too, only it gets finds all of the joints.

local character=script.Parent
character.Humanoid.BreakJointsOnDeath = false
character.Humanoid.Died:Connect(function()
	local d = character:GetDescendants()
	for i=1,#d do
		local desc = d[i]
		if desc:IsA("Motor6D") then
			print(desc)
			local socket = Instance.new("BallSocketConstraint")
			local part0 = desc.Part0
			local joint_name = desc.Name
			local attachment0 = desc.Parent:FindFirstChild(joint_name.."Attachment") or desc.Parent:FindFirstChild(joint_name.."RigAttachment")
			local attachment1 = part0:FindFirstChild(joint_name.."Attachment") or part0:FindFirstChild(joint_name.."RigAttachment")
			if attachment0 and attachment1 then
				socket.Attachment0, socket.Attachment1 = attachment0, attachment1
				socket.Parent = desc.Parent
				desc:Destroy()
			end	
		end
	end
end)

Is there anything I can do to fix it?

2 comments

0

Use an in pairs loop instead of a for loop.

0

Still has the same issue

1 answer

I figured out the issue. The parts wouldn't be loaded in time when desc is defined, so a wait() just had to be out in before it was.

This discussion is preserved as it was. New answers, comments, and votes are closed.