Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How to center a gui?

Asked by 10 years ago

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

Log in to vote
1
Answered by
52M 10
10 years ago

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.

Ad
Log in to vote
0
Answered by
Tesouro 407 Moderation Voter
10 years ago

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.

0
I did this, and now the GUI is always in the corner. Octillerysnacker 115 — 9y
0
So the script isn't running. Put it in a loop, in a local script, anything... Tesouro 407 — 9y
0
Try inserting a print("anything") and keep your eyes in the output. Tesouro 407 — 9y
Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

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...

Answer this question