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

local items = {
    "item1", "item2", "item3",
}

local gui = script.Parent

for i = 1, #items do
    print(i)
    local slot = Instance.new('TextButton',gui.Frame)
    slot.Name = items[i]
    slot.Text = items[i]    
    slot.Size = UDim2.new(0.15,0,1,0)
    slot.Position = UDim2.new((0.16*i)-0.16,0,0,0) --How do i fix this line?
end
0
What do you mean by centre? How do you want to lay them out? BlackJPI 2658 — 8y
0
Just use my centerX function on every gui theCJarmy7 1293 — 8y

3 answers

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

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

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

if this didn't help you, leave comment.

0
Yes, guis not a single gui jordan0810 55 — 8y
Ad
Log in to vote
0
Answered by 8 years ago
local items = {
    "item1", "item2", "item3",
}

local gui = script.Parent

for i = 1, #items do
    print(i)
    local slot = Instance.new('TextButton',gui.Frame)
    slot.Name = items[i]
    slot.Text = items[i]    
    slot.Size = UDim2.new((1/#items),0,1,0)
    slot.Position = UDim2.new((i * (1/#items),0,0,0) -- This doesnt leave a gap.
end

0
Should be `(i - 1)*(1/#items)` as the first should be positioned at (0, 0, 0, 0) BlackJPI 2658 — 8y
0
that still does not quite work ;( jordan0810 55 — 8y
Log in to vote
0
Answered by
Sublimus 992 Moderation Voter
8 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.

local items = 4 -- Number of buttons you are creating
local split = 1/(items) -- Get x-scale of button
local buffer = split/2  -- Size of buffer
local size = UDim2.new(split-buffer, 0, 1, 0) -- You can change the y-scale to what you like.
local place = buffer/2 -- You'll understand in a second

for i = 1, items do -- For each of the items
    local but = Instance.new("TextButton", script.Parent) -- Create a textbutton in the frame
    but.Name = "Item"..tostring(i) -- Item1, Item2, etc...
    but.Size = size
    but.Position = UDim2.new(place,0,0,0) -- Place at defined position
    place = place + (buffer * 2)  -- Set the starting place of the next button
end

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

Answer this question