Setting Parent of a Frame --> Clones for some reason!

I am currently working on a simple inventory system where you can drag and drop items. I created a GridLayout and if I want to move the item, my script sets the parent to Background so I can control the position of the item.

My problem is that, despite correctly setting the parent to the background, it somehow also clones the frame so when my input ends, setting the parent back will result in having one more frame.

I noticed that if I just define the frame and then set the parent, it is not happening but when changing the button, it somehow does that weird cloning of the frame. I don’t understand how this is even possible so here I am.

Anyone knows what the problem might be?

Here are the most important information about my script and the structure:

From in-game: (Notice how Background has a child when I hold it but it removes once I let go)


Script:

UserInputService.InputBegan:Connect(function(input, gameProcessedEvent)
	if input.UserInputType == Enum.UserInputType.MouseButton1 then
		local mousePosition = input.Position
		local buttons = StarterGui:GetGuiObjectsAtPosition(mousePosition.X, mousePosition.Y)

		if buttons then
			for _, button in pairs(buttons) do
				if button.Name == "Slot" or button.Name == "Slot1" or button.Name == "Slot2" or button.Name == "Slot3" or button.Name == "Slot4" or button.Name == "Slot5" then
					-- Set Variables:
					status["isChanging"] = true
					status["ObjectChanging"] = button
					
					button.Parent = Background
					status["ObjectChanging"].Position = UDim2.new(0,status["ObjectChanging"].AbsolutePosition.X,0,status["ObjectChanging"].AbsolutePosition.Y)
				end
			end
		end
	end
end)

UserInputService.InputEnded:Connect(function(input, gameProcessedEvent)
	if input.UserInputType == Enum.UserInputType.MouseButton1 then
		-- Change Parent
		if status["ObjectChanging"] ~= false then
			status["ObjectChanging"].Parent = InventoryFrame -- changing parent back to InventoryFrame so it is influenced by its organization	
		end

		-- Set Status:
		status["isChanging"] = false
		status["ObjectChanging"] = false
	end
end)

local function changeObject()
	status.ObjectChanging.Position = UDim2.new(0, mouse.X, 0, mouse.Y)
end



mouse.Move:Connect(function()
	if status["isChanging"] then
		changeObject()
	end
end)

Any help is appreciated!
(I really don’t want to define each slot, check if it is clicked on and then move it… There must be a more elegant way)

Found the solution here:

All GUIs returned by BasePlayerGui:GetGuiObjectsAtPosition() are ‘cloned’ by the highlightAsFrame() local function, which creates a Frame GUI positioned on top of the specified GUI that is semi-transparent and the same GuiBase2d.AbsoluteSize and GuiBase2d.AbsolutePosition.

The reason why it is cloning is because this is how the function works. To fix my issue, I named all parts differently and then search the real instance of the button:

local buttonOriginal = InventoryFrame:FindFirstChild(button.Name)

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