Size becoming 0,0,0,0 when gui duplicated

Hello, this is my code for automatically turning all new startergui descendants to Scale size.

local startergui = game.StarterGui

function ToScale(guiobject)
local Viewport_SizeX = workspace.CurrentCamera.ViewportSize.X
local Viewport_SizeY = workspace.CurrentCamera.ViewportSize.Y
local LB_SizeX = guiobject.AbsoluteSize.X
local LB_SizeY = guiobject.AbsoluteSize.Y
local ScaleX = LB_SizeX/Viewport_SizeX
local ScaleY = LB_SizeY/Viewport_SizeY
guiobject.Size = UDim2.new(ScaleX,0,ScaleY, 0)
end

startergui.DescendantAdded:connect(function(v)
    if v:IsA("GuiObject") and not v:IsA("ScreenGui") and not v:IsA("Folder") and not v:IsA("StarterGui") and script.Parent.Value == true then
    --Convert to Scale
    ToScale(v)
    end 
end)

It works, but when i duplicate something in startergui, the size becomes 0,0,0,0 for some reason

I’ve done some testing and was able to recreate the problem, It doesnt seem to be that your script is the problem and I’ve ruled out any plugins I have that could effect it so its most likely a roblox bug I suggest contacting a Top Contributor if you don’t have access to studio bugs.

This is because you are calculating the size using AbosluteSize. AbsoluteSize reads 0,0 when there is no parent, and a few other cases that aren’t coming to mind right now… You need to calculate the size from the original GuiObject, or after you parent the new clone.

2 Likes

Ok thanks! So I just see if the guiobject has a parent and then take the parents absolute size?

I’d be careful that your script also makes it scale relative to the viewport, when in reality GUI children scale to their parent. This means if you had a viewport of 1920x1080 and have a frame of 0.5,0,0.5,0 and then ‘convert to scale’ a child of the frame with size 0,960,0,540 it would end up being 480x270 after conversion, which is not intended based on your description.

You should convert using FindFirstAncestorWhichIsA( ‘GuiObject’ ) as the base.

3 Likes

I did what you asked but I’m still having the same issue.

local startergui = game.StarterGui

function ToScale(guiobject)
local Viewport_Size = workspace.CurrentCamera.ViewportSize
local LB_Size

local AncestorGUIObj = guiobject:FindFirstAncestorWhichIsA("GuiObject")
if AncestorGUIObj and guiobject.Parent then
   LB_Size = AncestorGUIObj.AbsoluteSize
else
   LB_Size = guiobject.AbsoluteSize
end

guiobject.Size = UDim2.new(LB_Size.X/Viewport_Size.X,0,LB_Size.Y/Viewport_Size.Y, 0)
print(LB_Size.X.." / "..Viewport_Size.X)
end

startergui.DescendantAdded:connect(function(v)
	if v:IsA("GuiObject") and not v:IsA("ScreenGui") and not v:IsA("Folder") and not v:IsA("StarterGui") and script.Parent.Value == true then
	--Convert to Scale
	ToScale(v)
	end 
end)

Printing AncestorGUIObj.AbsoluteSize.X is 0

As mentioned in my post, it’s the Viewport size that is the problem. If your GuiObject is nested within another, then you need to use its parent’s AbsoluteSize as the ViewportSize. You shouldn’t have changed LB_Size.

In terms of it printing 0, try adding game:GetService( 'RunService' ).RenderStepped:Wait() at the top of your ToScale function.

2 Likes

Thanks for the help, it works now :slight_smile:

It’s one of those annoying Roblox-isms where it takes 1 render step after parenting for the AbsoluteSize to be set. Weird but you’ll run into similar issues from time to time with properties like this which are the result of rendering. Glad it works!

1 Like