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

How can i make this script center multiple guis?

Asked by 9 years ago

Hey guys, i'm trying to make a script that creates and centers x amount of guis for x amount of "items" The current code displays and creates the 3 guis, but i'm unsure how to go about centering them.

Thanks for looking and i hope someone can help

EDIT:

Due to many people being unable to help me because of my lack of explanation here is some more details about what i'm trying to do.

I have a long frame and i want to fit in #items amount of smaller frames in it which are equally spread out filling the longer frame

Thanks again

01local items = {
02    "item1", "item2", "item3",
03}
04 
05local gui = script.Parent
06 
07for i = 1, #items do
08    print(i)
09    local slot = Instance.new('TextButton',gui.Frame)
10    slot.Name = items[i]
11    slot.Text = items[i]   
12    slot.Size = UDim2.new(0.15,0,1,0)
13    slot.Position = UDim2.new((0.16*i)-0.16,0,0,0) --How do i fix this line?
14end
0
What do you mean by centre? How do you want to lay them out? BlackJPI 2658 — 9y
0
Just use my centerX function on every gui theCJarmy7 1293 — 9y

3 answers

Log in to vote
0
Answered by
theCJarmy7 1293 Moderation Voter
9 years ago

Ok, I think you're trying to center gui's.

1function centerX(gui)
2    local y = gui.Position.Y
3    gui.Position = UDim2.new(.5,-gui.Size.X.Offset/2,y.Scale,y.Offset)
4    --[[tada!
5So, gui's get centered by making it's scale .5, and it's offset negative half of it's size Offset.
6--]]
7end

if this didn't help you, leave comment.

0
Yes, guis not a single gui jordan0810 55 — 9y
Ad
Log in to vote
0
Answered by 9 years ago
01local items = {
02    "item1", "item2", "item3",
03}
04 
05local gui = script.Parent
06 
07for i = 1, #items do
08    print(i)
09    local slot = Instance.new('TextButton',gui.Frame)
10    slot.Name = items[i]
11    slot.Text = items[i]   
12    slot.Size = UDim2.new((1/#items),0,1,0)
13    slot.Position = UDim2.new((i * (1/#items),0,0,0) -- This doesnt leave a gap.
14end
0
Should be `(i - 1)*(1/#items)` as the first should be positioned at (0, 0, 0, 0) BlackJPI 2658 — 9y
0
that still does not quite work ;( jordan0810 55 — 9y
Log in to vote
0
Answered by
Sublimus 992 Moderation Voter
9 years ago

Okay, so this leaves a buffer on both the right and left sides of the containing GUI that is half the size of the buffer between the elements. If this is unsatisfactory, I can rewrite it so that they touch both sides.

01local items = 4 -- Number of buttons you are creating
02local split = 1/(items) -- Get x-scale of button
03local buffer = split/2  -- Size of buffer
04local size = UDim2.new(split-buffer, 0, 1, 0) -- You can change the y-scale to what you like.
05local place = buffer/2 -- You'll understand in a second
06 
07for i = 1, items do -- For each of the items
08    local but = Instance.new("TextButton", script.Parent) -- Create a textbutton in the frame
09    but.Name = "Item"..tostring(i) -- Item1, Item2, etc...
10    but.Size = size
11    but.Position = UDim2.new(place,0,0,0) -- Place at defined position
12    place = place + (buffer * 2-- Set the starting place of the next button
13end

Edit: If you need help rewriting it to fit your specific example, I can do that as well.

Answer this question