Hello, I’m trying to make a local plugin which converts my GUI to scale when I first add it to the game but I am having sizing inconsistencies.
here is my code:
local desirables = {"TextLabel", "TextButton", "Frame", "ImageLabel", "ImageButton", "TextBox", "ScrollingFrame", "VideoFrame", "ViewportFrame"}
local function scale(x, y, screenGui)
local absoluteSize = screenGui.AbsoluteSize
return UDim2.new(x/absoluteSize.X, 0, y/absoluteSize.Y, 0)
end
local function findFirstParentWithClass(object, className)
local currentParent = object.Parent
while currentParent do
if currentParent:IsA(className) then
return currentParent
end
currentParent = currentParent.Parent
end
return nil -- Return nil if no parent with the specified class is found
end
local function scaleObject(obj, scree)
if table.find(desirables, obj.ClassName) then
local offsetSize = scale(obj.Size.X.Offset, obj.Size.Y.Offset, findFirstParentWithClass(obj, "ScreenGui"))
local offsetPos = scale(obj.Position.X.Offset, obj.Position.Y.Offset, findFirstParentWithClass(obj, "ScreenGui"))
if obj.Size.X.Offset ~= 0 or obj.Size.Y.Offset ~= 0 then
obj.Size = UDim2.new(offsetSize.X, 0, offsetSize.Y, 0)
end
if obj.Position.X.Offset ~= 0 or obj.Position.Y.Offset ~= 0 then
obj.Position = UDim2.new(offsetPos.X, 0, offsetPos.Y, 0)
end
end
end
game.StarterGui.DescendantAdded:Connect(function(obj)
scaleObject(obj)
end)
for _, obj in pairs(game.StarterGui:GetDescendants()) do
scaleObject(obj)
end
as you can see, the script should be converting the sizes and positions to scale, however lets see what actually happens:
Original size:
Converted size:
As you can see, it makes it close to 10x smaller for some reason. What is wrong with the code?