My rig builder module doesn't build a rig? Can't see the issue

Hello people of devforum, I have a small issue with a module
I am currently working on.

I am making a module that cycles through the whole model, collects every attachment, and creates a Motor6D.

But for some reason the function doesn’t work.
It seperates attachments into 2 groups, with and without underscrore (_).
the underscrore is there to tell the module which part should be the Part0 and which the Part1 of the Motor6D weld.

My module doesn’t give any errors and states that the rig is build.
But everytime I check the model, the rig isn’t build at all, all Motor6D are supposed to be either parented inside the Part1 or parented in a folder
named “joints”.
Both are empty.

I can’t spot the error anywhere and I have tried several prints inside each function but no success/clue.

Perhaps I didn’t look deep enough into it, but with my current brain power, I can’t figure out the issue myself.
The module looks like as if it would function perfectly fine.

The entire module below. \ /

local mod = {}

local cf = CFrame.new

--require(game.storage.sharedcode.jointmaker).buildrig(workspace.testrig)

local function getatts(obj, list1, list2)
	if obj then
		if obj.ClassName == "Attachment" then
			local name = obj.Name
			if name:match("_") ~= nil then
				list2[name] = obj
			else
				list1[name] = obj
			end
		end
		--------------------------------------------
		if obj.ClassName == "Part" then
			for i, v in ipairs(obj:GetChildren()) do
				local temp = getatts(obj, list1, list2)
			end
		end
	end
end




function makejoint(a0, a1, par)
	local joint = Instance.new("Motor6D")
	joint.Name = a0.Parent.Name .. "_joint"
	joint.C0 = cf(a0.Position)
	joint.C1 = cf(a1.Position)
	joint.Part0 = a0.Parent
	joint.Part1 = a1.Parent
	--
	joint.Parent = par
end


mod.buildrig = function(model)
	local atts0, atts1 = {}, {}
	getatts(model, atts0, atts1)
	
	for i, a0 in pairs(atts0) do
		local name = i .. "_"
		local a1 = atts1[name]
		if a1 ~= nil then
			makejoint(a0, a1, model.joints)
		end
	end
	
	print("Rig builded.")
end

return mod
1 Like

I noticed a small bug in your code. In your function getatts, you are passing in the original obj when you should be passing in v:

--[[ replace this
if obj.ClassName == "Part" then
	for i, v in ipairs(obj:GetChildren()) do
		local temp = getatts(obj, list1, list2)
	end
end

]]-- with this
if obj.ClassName == "Part" then
	for k, v in ipairs(obj:GetChildren()) do
		local temp = getatts(v, list1, list2)
	end
end

That’s all I can spot right now though.

1 Like

Oh yes! Thank you!
Might also have caused the infinite loop/stack overflow which is why I added a ClassName == “Part” to it.

I rewrote the entire function before you responded which fixed it eventually, but I’m still going to thank you and mark the solution.

Your help is appreciated and could’ve fixed it if I didn’t decide to rewrite the entire module.

1 Like