Getting Children of Model giving me an error but still working inside loop?

  1. What do you want to achieve?
    Currently just playing around and making a script that weld constraints the parts inside a model to a part.
    I have a model which is a ghost that follows you around. Inside the model I have a part called “Root”, and another model named “Part” which has a bunch of parts inside. For it to follow you around, it needs to weld constraint the part “Root” which all the children inside the model “Parts”. I want to practice automating tasks which would otherwise need to do manually. However I get this annoying bug. First off here is what I have in code so far.
    Currenty I have this script which is inside the “Root” part inside the “Ghost” model.
local root = script.Parent
for x, part in pairs(root.Parent.Parts:GetChildren()) do
	local weld = Instance.new("WeldConstraint")
	weld.Parent = root
	weld.Part0 = root
	weld.Part1 = part
end

Heres the code for the ghost to follow you:

function giveGhost(player)
	if player then
		local character = player.Character
		if character then
			local humRootPart = character.HumanoidRootPart
			local newGhost = root:Clone()
			newGhost.Parent = character

			local bodyPos = Instance.new("BodyPosition", newGhost)
			bodyPos.MaxForce = Vector3.new(math.huge, math.huge, math.huge)

			local bodyGyro = Instance.new("BodyGyro", newGhost)
			bodyGyro.MaxTorque = Vector3.new(math.huge, math.huge, math.huge)

			while wait() do
				bodyPos.Position = humRootPart.Position + Vector3.new(2, 2, 3)
				bodyGyro.CFrame = humRootPart.CFrame
			end
		end
	end
end

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(char)
		giveGhost(player)
	end)
end)
  1. What is the issue?

It loops through perfectly through the “Part” model which contains all of the ghost’s parts, and successfully WeldConstraints root which each child inside “Part”, however I get an error every time
"Parts is not a valid member of Model “Workspace.oeflor” "

It makes no sense because if it didn’t find the Parts model, why did it still successfully loop through all its children. Is there something I am doing wrong or missing?

1 Like

From which line of the code the error is being produced? It’d be helpful if you bring the exact line that produces the error.

Line 2
“for x, part in pairs(root.Parent.Parts:GetChildren()) do”

Which is strange because it still loops through and welds the children and everything

Could you take a screenshot of your model Parts’ explorer ancestry?

Screenshot_4

Are those two codes are in one script? If so, could you provide the full code including the part where the iteration is being used?