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
01 | local items = { |
02 | "item1" , "item2" , "item3" , |
03 | } |
04 |
05 | local gui = script.Parent |
06 |
07 | for 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 = UDim 2. new( 0.15 , 0 , 1 , 0 ) |
13 | slot.Position = UDim 2. new(( 0.16 *i)- 0.16 , 0 , 0 , 0 ) --How do i fix this line? |
14 | end |
Ok, I think you're trying to center gui's.
1 | function centerX(gui) |
2 | local y = gui.Position.Y |
3 | gui.Position = UDim 2. new(. 5 ,-gui.Size.X.Offset/ 2 ,y.Scale,y.Offset) |
4 | --[[tada! |
5 | So, gui's get centered by making it's scale .5, and it's offset negative half of it's size Offset. |
6 | --]] |
7 | end |
if this didn't help you, leave comment.
01 | local items = { |
02 | "item1" , "item2" , "item3" , |
03 | } |
04 |
05 | local gui = script.Parent |
06 |
07 | for 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 = UDim 2. new(( 1 /#items), 0 , 1 , 0 ) |
13 | slot.Position = UDim 2. new((i * ( 1 /#items), 0 , 0 , 0 ) -- This doesnt leave a gap. |
14 | end |
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.
01 | local items = 4 -- Number of buttons you are creating |
02 | local split = 1 /(items) -- Get x-scale of button |
03 | local buffer = split/ 2 -- Size of buffer |
04 | local size = UDim 2. new(split-buffer, 0 , 1 , 0 ) -- You can change the y-scale to what you like. |
05 | local place = buffer/ 2 -- You'll understand in a second |
06 |
07 | for 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 = UDim 2. new(place, 0 , 0 , 0 ) -- Place at defined position |
12 | place = place + (buffer * 2 ) -- Set the starting place of the next button |
13 | end |
Edit: If you need help rewriting it to fit your specific example, I can do that as well.