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

How can i make this script center multiple guis?

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

2 comments

0

What do you mean by centre? How do you want to lay them out?

0

Just use my centerX function on every gui

3 answers

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.

1 comment

0

Yes, guis not a single gui

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

2 comments

0

Should be (i - 1)*(1/#items) as the first should be positioned at (0, 0, 0, 0)

0

that still does not quite work ;(

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.

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