How to keep gui pos and size when re-parented.. but in scale?

This might be impossible to do… What im trying to do was making a guiobject keep the pos and size as from the previous parent when reparented into another frame. Was trying to make loadout system tho

Here what i want to do:
image

Although i just read a post about the same thing but that’s for offset… Here’s the link: Keep Gui Position When Re-Parented?

However i tried doing that in different place file then tried to convert into scale, doesnt work though.
image

Here’s the code for it idk how to explain more lol

local screengui = script.Parent
local c = {
	frame1 = screengui.Frame1,
	frame2 = screengui.Frame2,
	red = screengui.Frame1.Red
}
local curRedPos = c.red.Position

local RS = game:GetService('RunService')

local timer = 0
local bool = false
RS.RenderStepped:Connect(function(dt)
	if timer > 1 then
		bool = not bool
		timer = 0
		if bool then
			c.red.Parent = c.frame2
			c.red = c.frame2.Red
			local h = (c.frame1.AbsolutePosition - c.red.AbsolutePosition) / screengui.AbsoluteSize
			-- tried this and still couldnt position to the frame1 as the scale
			-- about the size i really dont know how to figure that out :/
			c.red.Position = UDim2.new(h.x,0,h.y,0)
		else
			c.red.Parent = c.frame1
			c.red = c.frame1.Red
			c.red.Position = curRedPos
		end
	else
		timer += dt
	end
end)
3 Likes

Hey, I got notified about this because you mentioned my post, you could try using this:

-- From the post I found my solution in

local gui = ... -- some GUI
local desiredAbsolutePosition = -- some Vector2
local relativePosition = desiredAbsolutePosition - gui.Parent.AbsolutePosition
gui.Position = UDim2.fromOffset(relativePosition.X, relativePosition.Y)

Then change UDim2.fromOffset to UDim2.fromScale.

If that doesn’t work you could use a function like this to change the offset to scale:

local function OffsetToScale(Offset)
	local ViewPortSize = workspace.Camera.ViewportSize
	return ({Offset[1] / ViewPortSize.X, Offset[2] / ViewPortSize.Y})
end

about the local relativePosition = desiredAbsolutePosition - gui.Parent.AbsolutePosition that does not work, the red frame basically becomes slower depends on the parent size. Plus using the / screengui.AbsoluteSize it does not work it exactly wanted. Try that in new project, add a screengui and red frame inside a small frame, try making the red frame scale position be 1

Nevermind just figured it out by reading this: Making a GUI Object maintain its position when parented to another - #2 by AGIT_Games

Here’s the code if anyone wants to know how to do it (Edit: updated the script just to fix the position bug)

local screengui = script.Parent
local c = {
	frame1 = screengui.Frame1,
	frame2 = screengui.Frame2,
	red = screengui.Frame1.Red
}
local curRedPos = c.red.Position
local curRedSize = c.red.Size
local curAbsRedPos = c.red.AbsolutePosition
local curAbsRedSize = c.red.AbsoluteSize

local RS = game:GetService('RunService')

local timer = 0
local bool = false
RS.RenderStepped:Connect(function(dt)
	if timer > 1 then
		bool = not bool
		timer = 0
		if bool then
			c.red.Parent = c.frame2
			c.red = c.frame2.Red
			local h = (curAbsRedPos - c.red.AbsolutePosition) / c.frame2.AbsoluteSize
			c.red.Position = curRedPos + UDim2.new(h.X,0,h.Y,0)
			local siz = (curAbsRedSize - c.red.AbsoluteSize) / c.frame2.AbsoluteSize
			c.red.Size = curRedSize + UDim2.new(siz.X,0,siz.Y,0)
		else
			c.red.Parent = c.frame1
			c.red = c.frame1.Red
			c.red.Position = curRedPos
			c.red.Size = curRedSize
		end
	else
		timer += dt
	end
end)
2 Likes