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

Why can't I refer to the parent of a child?

So I'm making a GUI backpack script, and it works by creating a button for every part in the "pack" which is a model in Players.LocalPlayer. My problem is, when my update function runs (the function that actually makes the buttons) it keeps making a button for the same item in the pack every time it runs. I am trying to make a second pack so that I can move the items to that pack once a button is created for it, but I'm having trouble moving them from pack to pack2. The only way I can think of referring to the item that I want to move is child, since this is all happening in a "for index, child in pairs(pack_tab) do" When I refer to the child it says " bad argument #3 to 'Parent' (Object expected, got string)" So I guess to simplify my question, why can't i refer to the parent of a child, and how can I get around this problem? here is the function I'm talking about below. Let me know if you have any questions because I know this is kind of complicated and confusing. Thanks!

function Update()
	local player = game.Players.LocalPlayer
	local char = player.Character
	local pack = player:findFirstChild("Pack")
	local pack2 = player:findFirstChild("Pack2")
	local pack_tab = pack:GetChildren()
	local Ipack_tab = IFRAME:GetChildren()

	if CURRENT == nil then
		local x = 0
		local y = 0

		
		for index, child in pairs(pack_tab) do
			local CHOICE_GUI = MFRAME.TextButton:clone()
CHOICE_GUI.Size = UDim2.new(0, 100, 0, 25)
			local new_gui = CHOICE_GUI 
			new_gui.Position = UDim2.new(0, x, 0, y)
			new_gui.Text = child.Name
			new_gui.Name = child.Name
			new_gui.MouseButton1Down:connect(function() onGuiClick(child) end)
			new_gui.Parent = IFRAME
			new_gui.Visible = true
			
			y = y + 25
			
			if y > 350 then
				y = 20
				x = x + 100
				
			end
			local CHILD = pack:FindFirstChild(child)
			CHILD.Parent = "Pack2"
		end
			
		--cleanGui()
	end
	end

1 answer

✓ Accepted answer

Just as output says, you're referring to a string when you should be referring to an object - in this case, the variable pack2. It seems to me that line 33 just needs to be changed to

CHILD.Parent = pack2

since you mistakenly used the string "Pack2" instead of the variable pack2.

Doesn't seem to be a hierarchy problem to me!

3 comments

0

now the error says that CHILD is a nil value...

1

That means the FindFirstChild method didn't find anything. Try pack:FindFirstChild(child.Name) instead, since that method looks for a string and not an object.

0

that worked, thank you so much!

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