Gui Scale to Offset function

Im currently having a issue with my Gui script that translates the scale into offset

At the moment no matter what it just makes it {0,0} {0,0}
The ViewportSize is the available space on the players screen.
There are no error messages.

I’m using scale and have a frame that covers 0.25 of my screen on the x axis so if my resolution is 1366 x 768 it should be 341.5 when translated.

Im not really sure about whats up with it other than the fact the Udim2 always comes out as 0s

function GetTranslated(Udim2)
	local ViewportSize = GetCurrentCam().ViewportSize
	
	local NewX = Udim2.X.Scale * ViewportSize.X
	local NewY = Udim2.Y.Scale * ViewportSize.Y
	print(Udim2.X.Scale)
	
	local NewPosition = UDim2.new(0,Udim2.X.Scale,0,Udim2.Y.Scale)
	print(NewPosition)
	return NewPosition
end
1 Like

I hope you know that you are building the NewPosition with the original scale values, not NewX or NewY

I just found that and have changed it, its still making my gui dissapear from screen though

Could that have anything to do with how im setting it?

function TranslateGui(Gui)
	local Des = Gui:GetDescendants()
	for i = 1,#Des do
		local S,R = pcall(function()
			Des[i].Size = GetTranslated(Des[i].Size)
			Des[i].Position = GetTranslated(Des[i].Position)
		end)
	end
end

Edit:
I its getting the correct values now as shown by my output:

  {0, 778}, {0, 415} ---> {0, 0}, {0, 0}
  {0.267399281, 0}, {0.213541672, 0} ---> {0, 365}, {0, 164}
  {0, 766}, {0, 401} ---> {0, 0}, {0, 0}
  {0.00642673532, 0}, {0.01686747, 0} ---> {0, 8}, {0, 12}
  {0, 700}, {0, 100} ---> {0, 0}, {0, 0}
  {0, 0}, {0, 0} ---> {0, 0}, {0, 0}
  {0, 734}, {0, 76} ---> {0, 0}, {0, 0}
  {0.0157142859, 0}, {0.119999997, 0} ---> {0, 21}, {0, 92}

In the function, you are not returning from either line.

Des[i].Size = GetTranslated(Des[i].Size)
Des[i].Position = GetTranslated(Des[i].Position)

You are setting the size I can see, but why do you have S, R stored from the return?

They are what I use with pcall as i will have to allow for scripts being in the gui,
im trying an adaption now

Still the same with this:

local S,R = pcall(function()
			if typeof(Des[i].Size) == "UDim2" then
				-- No errors
			else
				-- Make error
				local M = 123 + Vector3.new(0,0,0)
			end
		end)
		if S then
			Des[i].Size = GetTranslated(Des[i].Size)
			Des[i].Position = GetTranslated(Des[i].Position)
		end

Adaption did not work.
function GetTranslated(udim2)
    local ViewportSize = GetCurrentCam().ViewportSize
    local NewX = Udim2.X.Scale * ViewportSize.X + udim2.X.Offset
    local NewY = Udim2.Y.Scale * ViewportSize.Y + udim2.Y.Offset

    return UDim2.new(0, NewX, 0, NewY)
end

You’re not actually using the new values and instead using the scales in the offset position which can’t take floats so it rounds it down to the nearest whole number, which is 0

1 Like

The rounding isn’t the issue. The issue is that the gui becomes invisible. At no point to I disable it or change transparency.

The gui is there but its not on the screen after changing the size and position

Edit:

Ive found the fix. The issue was that it wasnt checking if it already had offset.
this means any that were already using offset would be set to 0