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

Make a ScreenGui center on all screens?

Asked by 10 years ago

How would i center a ScreenGui or anything within? Lets say i have a logo, and i want it to be at the CENTER of every players screen no matter what screen size/resolution. How would this be performed?

Not a request. More like... advice.

2 answers

Log in to vote
1
Answered by
adark 5487 Badge of Merit Moderation Voter Community Moderator
10 years ago

This isn't a scripting request, we're not making anything for you. :P

(For clarification, we only consider something a "request" when the asker (you, in this case) request us to write code for you without any effort put in on their to make the code first.)

When you center something, you want to set the Scale portions of the Position to .5, for both X and Y. This puts the top-left pixel of the GUI in the exact center of the screen.

To make this actually centered, you have to Offset the Position by half of the Size of the GUI.

For example:

function Center(gui)
    gui.Position = UDim2.new(.5, -gui.AbsoluteSize.X/2, .5, -gui.AbsoluteSize.Y/2)
end

If the Size's components are odd, this results in a split pixel, so the GUI will be off center by a single pixel. Make sure the components are even to avoid this. :)

0
Thanks. I'm a bit confused by this but i'm sure i'll get it. Also, i'm your 100th up-voter. Congrats ;) RobloxHelper49 55 — 10y
0
It'll make more sense if you set the Scale portions only, and then apply the offsets by hand. And don't forget to mark my answer as accepted! :) adark 5487 — 10y
Ad
Log in to vote
1
Answered by 10 years ago

UDim2's have four values: x.scale, x.offset, y.scale, and y.offset, where UDim2.new(0, 0, 0, 0) is the top left corner. To center a GUI, use UDim2.new(0.5 , -X/2, 0.5, -Y/2) where X is the x component and Y is the y component of the Absolute Size of the GUI to be centered.

UDim2 documentation

Answer this question