How to start from the primary part when looping over model children

So i have a script which is SUPPOSED TO weld a model from the primary part to the other end of the model, but i have no idea on how to start looping at the primary part

this is the current code (it is inside of a module)

function weld:WeldOld(model:Model)
	local model1 = model:GetChildren()
	for i,a:Part in pairs(model1) do
		if a:GetAttribute("yes") == nil then
			a:SetAttribute("yes", true)
			print(a.Name)
			task.spawn(function()
				local iterateover = workspace:GetPartBoundsInBox(a.CFrame, a.Size + Vector3.new(0.01,0.01,0.01))
				for i,v in pairs(iterateover) do
					if v.Parent ~= model then continue end
					--if i == #model1 then print("done") end
					local weld = Instance.new("WeldConstraint", a)
					weld.Part0 = a
					weld.Part1 = v
				end
			end)
		end
		task.wait()
	end
end
1 Like

I’m extremely confused as to what your end goal is, but I was able to whip up a function for making a “loop” through a model’s children start with its PrimaryPart. It takes a model and a function to run on each child, runs the function on the PrimaryPart first, then runs it on everything else.

local function ModelLoopStartWithPrimaryPart(Model,Func)
	if not Model.PrimaryPart then error("Model does not have PrimaryPart") end
	Func(Model.PrimaryPart)
	for i,v in pairs(Model:GetChildren()) do
		if v == Model.PrimaryPart then continue end
		Func(v)
	end
end

--// Example usage
ModelLoopStartWithPrimaryPart(workspace.Model,function(v) --// v is a model child instance
	print(v.Name)
end)
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.