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

Make a ScreenGui center on all screens?

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

✓ Accepted answer

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

2 comments

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 ;)

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! :)

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

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