of     2   
chevron_rightchevron_rightchevron_right

Klink45
#194228186Thursday, July 21, 2016 10:28 PM GMT

Every time I try to center a GUI sing formulas all over the Internet I get it wrong. All I want is for my GUIs to stay put! Part of that is centering them. How do I make my frame be in the absolute middle of the screen, regardless of screen resolution? u sicko!
RemasteredBox
#194228394Thursday, July 21, 2016 10:30 PM GMT

function centerGui(gui) local absSize = gui.AbsoluteSize gui.Position = UDim2.new(0.5, absSize.X/2, 0.5, absSize.Y/2) end Untested #code print("While I may look like a cataclysmic god of the eggs from the future, I am not.")
RemasteredBox
#194228477Thursday, July 21, 2016 10:31 PM GMT

Oops I made a few mistakes. Here's a cleaned up & fixed version. #code local function centerGui(gui) local absoluteSize = gui.AbsoluteSize gui.Position = UDim2.new(0.5, -(absoluteSize.X / 2), 0.5, -(absoluteSize.Y / 2)) end #code print("While I may look like a cataclysmic god of the eggs from the future, I am not.")
Klink45
#194228801Thursday, July 21, 2016 10:35 PM GMT

@Remastered That was nice of you! Thanks! I also now know the formula... >:) u sicko!
RemasteredBox
#194228912Thursday, July 21, 2016 10:36 PM GMT

No problem. #code print("While I may look like a cataclysmic god of the eggs from the future, I am not.")
Klink45
#194229465Thursday, July 21, 2016 10:43 PM GMT

Is it possible to make that code work for frames inside of frames? I tried it in a frame inside a frame and it centered it in the frame but not the player's screen. I don't want to change the frame's parent because I have a lot of scripts referencing it. u sicko!
RemasteredBox
#194232007Thursday, July 21, 2016 11:14 PM GMT

Hmm yea, give me a minute. #code print("While I may look like a cataclysmic god of the eggs from the future, I am not.")
Klink45
#194249892Friday, July 22, 2016 3:15 AM GMT

rip u sicko!
TimeTicks
#194250982Friday, July 22, 2016 3:29 AM GMT

just set the position based on its parents position instead of itself
BusyCityGuy
#194251361Friday, July 22, 2016 3:35 AM GMT

As in the formula, the basic idea of centering is a combination of offset and scale. GUIs are positioned from their top left pixel. This means if you place it at (.5, 0, .5, 0) Then the top left pixel will be in the center of your screen. To get the middle of the GUI in the middle of the screen, you need to move the gui up to the halfway point of the gui and left to the halfway point of the gui. This is accomplished by getting the absolute size and dividing it by two, then using that (negated) as the offset value. Pseudo formula: (0.5, -absSize.X/2, .5, -absSize.Y/2) Hopefully this explains what he did a little bit. -The [Guy]
RemasteredBox
#194251937Friday, July 22, 2016 3:42 AM GMT

Busy was spot on. @Klink Sorry, I tried and couldn't figure it out. What you can do is then scale the position down/up based on the parent frame's size. #code print("While I may look like a cataclysmic god of the eggs from the future, I am not.")
Klink45
#194252147Friday, July 22, 2016 3:45 AM GMT

THANK YOU BUSY u sicko!
Klink45
#194252305Friday, July 22, 2016 3:48 AM GMT

I tried this: local absoluteSize = script.Parent.Parent.Parent.AbsoluteSize--The actual GUI script.Parent.Position = UDim2.new(0.5, -(absoluteSize.X / 2), 0.5, -(absoluteSize.Y / 2))--The Frame And it didn't center my frame. Am I doing something wrong? u sicko!
BusyCityGuy
#194256817Friday, July 22, 2016 4:54 AM GMT

That will center it in whatever the parent of the frame is. Do you want it centered in the scope of the whole screen? -The [Guy]
BusyCityGuy
#194256843Friday, July 22, 2016 4:54 AM GMT

I spoke wrong - that will center the frame in the main GUI. -The [Guy]
Klink45
#194256916Friday, July 22, 2016 4:55 AM GMT

I want it centered in the whole screen, please. u sicko!
SpeedySeat
#194257900Friday, July 22, 2016 5:15 AM GMT

function ActualCenter(gui) return UDim2.new(0.5-(gui.Size.X.Scale/2), -gui.Size.X.Offset/2, 0.5-(gui.Size.Y.Scale/2), -gui.Size.Y.Offset/2 ) end While using Absolute is an option, it is always better to stay safe and rely on the actual element numbers.
LateralLace
#194257939Friday, July 22, 2016 5:16 AM GMT

Btw parent must be a GuiBase, so most above solutions wouldnt work cause of this. Here's a better solution: function CenterGuiBase(gui) PortSize = workspace.CurrentCamera.ViewPortSize pos = gui.AbsolutePosition size = gui.AbsoluteSize gui.Position = UDim2.new(((PortSize.X/2-size.X/2)/size.X*gui.Size.X.Scale),0,((PortSize.Y/2-size.Y/2)/size.Y*gui.Size.Y.Scale),0) end --[[ rip roblox inception ]]--
Klink45
#194258036Friday, July 22, 2016 5:18 AM GMT

@Speedy Didn't work. My frame is inside a frame if that helps. u sicko!
Klink45
#194258172Friday, July 22, 2016 5:20 AM GMT

@Lateral Centers my frame inside of my frame, but thanks. u sicko!
LateralLace
#194259920Friday, July 22, 2016 5:54 AM GMT

Well I'm a complete idiot who doesn't know how porpotions work -.- let's try this again: function CenterGuiBase(gui) local PortSize = workspace.CurrentCamera.ViewportSize local pos = {gui.AbsolutePosition} local size = gui.AbsoluteSize gui.Position = gui.Position + UDim2.new(.1,0,.1,0) table.insert(pos,gui.AbsolutePosition) gui.Position = UDim2.new(.1/(pos[2].X-pos[1].X)*(PortSize.X/2-size.X/2),0,.1/(pos[2].Y-pos[1].Y)*(PortSize.Y/2-size.Y/2),0) end --[[ rip roblox inception ]]--
LateralLace
#194260043Friday, July 22, 2016 5:57 AM GMT

Should work flawlessly. --[[ rip roblox inception ]]--
RemasteredBox
#194287960Friday, July 22, 2016 5:29 PM GMT

Speedy your dumb, "the actual elements". Lmao. #code print("While I may look like a cataclysmic god of the eggs from the future, I am not.")
SpeedySeat
#194293702Friday, July 22, 2016 6:53 PM GMT

"el·e·ment noun a part or aspect of something abstract, especially one that is essential or characteristic." Are you claiming that I was wrong in calling it the 'actual elements' instead of a screen region? Feel free to try and correct it. I don't mind fixing my vocabulary but an interface object does sound like an element to a game.
RemasteredBox
#194294086Friday, July 22, 2016 6:58 PM GMT

@Speedy I'm disagreeing with the terminology; it's not necessarily wrong, just you should call them the actual 'properties' for clarity. Also; AbsolutePosition and AbsoluteSize are COMPLETELY reliable, you should not use the actual values as that can take a bit more memory because you have to do more math. #code print("While I may look like a cataclysmic god of the eggs from the future, I am not.")

    of     2   
chevron_rightchevron_rightchevron_right