Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How to center a gui?

So I made a GUI with exact positions so it would fit in the center of my computer screen, but if I play my game on my brothers computer the GUI is in a different position. Is there any way to center the GUI universally?

3 answers

You would have to set the position using the scale coordinates instead of the Offset. For example:

GUI.Position = UDim2.new(.5, 0, .5, 0) -- Use the first set and don't touch the 2nd set.

You may have to play around with it because .5 would only work if the GUI was a very small size.

You have to consider the size of the Gui object too, so use this in your script:

x = (ScreenGui.AbsoluteSize.X - GuiObject.AbsoluteSize.X) / 2 -- here you take the size of the screen, subtract the size of your object and you divide by 2, so the object is placed in the middle
y = (ScreenGui.AbsoluteSize.Y - GuiObject.AbsoluteSize.Y) / 2 -- same with Y axis
GuiObject.Position = UDim2.new(0, x, 0, y) -- now join them in a UDim2 vector, the position of the object.

The origin of a Gui object is its top left corner, so you have to place it further up and left.

I made an graphic example to explain to you, have a look.

3 comments

0

I did this, and now the GUI is always in the corner.

0

So the script isn't running. Put it in a loop, in a local script, anything...

0

Try inserting a print("anything") and keep your eyes in the output.

Put it as 0.5 on the first x and y places, then put the size of the frame divided by two in the second places.

frame.Position = UDim2.new(0.5, frame.Size.X / 2, 0.5, frame.Size.Y / 2)

I know this is a year late and you're probably not active anymore but I figured why not help others who are trying to find this answer.

EDIT: Holy crap this is five years late, I didn't notice...

This discussion is preserved as it was. New answers, comments, and votes are closed.