Hey there, i need to know how i can change a GUI Element's (imagelabel, textlabel/button etc.) size from a script. Is it?
(GuiElement).Size = "{size}, {size}"
Please tell me
There is a data type called UDim2 for that, it has 3 ways of constructing the size, fromOffset, fromScale and new where you can specify both offset and scale.
(GuiElement).Size = UDim2.fromOffset(50, 100) -- Sets offset to 50, 100 (GuiElement).Size = UDim2.fromScale(0.25, 0.5) -- Sets scale to 0.25, 0.5 (GuiElement).Size = UDim2.new(0.25, 50, 0.5, 100) -- Sets scale to 0.25, 0.5 and offset to 50, 100
Offset = pixels, this means that fromOffset creates size based off of pixels, first parameter of it is X and the second is Y so doing UDim2.fromOffset(50, 100)
will set size of the box to 50 pixels X and 100 pixels Y, in new, offset is specified as every second argument
UDim2.new( 0.25, -- X Scale 50, -- X Offset 0.5, -- Y Scale 100 -- Y Offset )
The fromScale is something different and means size based off of it's parent, it you make text label with size of UDim2.fromScale(0.25, 1)
and put it into another label with size UDim2.fromOffset(50, 100)
, the label inside of it will have same size as 12.5 X, 100 Y
offset, that's because Y Scale was 1 which is full size of it's parent, it's parent had size 100 of offset so the child label was 100 too, X was 12.5 and that's because the parent label had X offset of 50, 50 * 0.25 = 12.5.
The difference between offset and scale is that offset is as i said pixels, this means if you have PC with size 1920x1080 pixels and you create TextLabel with offset size of 50, 50 it will look small for you because your screen is a lot bigger than that, but if you have the same size on Iphone 4, the label will look extremely big because Iphone 4 is very small.
That is why you should usually choose scale over offset, if you create a frame and parent it to ScreenGui, it's scale size will be based off of the screen size so if the frame would have size of 0.25, 1 of scale, on X it would take up 1/4 of the screen while on Y it would take up all space from top to bottom.