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)