Is there a better way I should be doing this? GUI Scripting optimization

I would like to find a better way to possibly make these tweens for my game UI. I’m new to scripting and would like a better optimized way to create UI if possible.

I feel like the scripts I have now are messy and possibly unoptimized.

Let me know if you have any suggestions for cleaner scripting.

Thanks!


1 Like

Although I’m far from qualified to give much feedback here, there are a few things that I would have done differently. Performance wise I have no clue how beneficial these are, so do what works for you.

First, since both conditions lead to the same tweeninfo, I would create that variable outside of the if statement so that we have one variable instead of 2. If we ever want to change it, we don’t have to worry about copying it to the other condition, unless of course, you wanted to make the open/close tweens different.

Second, since settingsMain.Visible is a bool value and can only be true or false, I would format the if statement like this:

if settingsMain.Visible then -- will do the same as settingsMain.Visible == true, but with less words
    settingsMain.Visible = false
    --do the rest of the function
else -- We already know that it's false since there can only be true or false, using else just means we won't have to check for it
    settingsMain.Visible = true
    --do the rest of the function
end

Third, even though we already changed this in the last point, it seems like you are using an if statement inside of the else portion. Not sure if you did this on purpose but you can use elseif instead of making a new if statement

if condition == 1 then

elseif condition == 2 then

end

If statements Documentation

Hopefully I was helpful to you and I’m glad you’re checking for a better way to be doing what you’re doing as that’s probably the question that got most of us into scripting in the first place. Once again I don’t know enough to know if this is performantly accurate and I don’t even know if what I’ve put here is best practice, it’s just what I would do. To anyone reading this that might know a better way as well, please tag me in the reply! :grin:

1 Like

I think the best thing you can do here, besides what GirthyBanana said above, is to use GuiObject:TweenPosition() instead of TweenService. GuiObjects like frames, text labels, etc. all have functions called TweenSize() and TweenPosition() which do exactly what they sound like; tween the object’s size/position. The difference between TweenPosition() and using tween service to tween the position is that tween service looks choppier, and TweenPosition() looks smoother. Same for TweenSize().

Here’s a code example of TweenPosition:

local frame = script.Parent -- A frame object

frame:TweenPosition(UDim2.new(.5, 0, .5, 0), "Out", "Linear", 1, true)

--TweenPosition's arguments are set up like this:
--[[
newPos - UDim2, the new position that the object will go to
easingDirection - EasingDirection, the easing direction of the tween
easingStyle - EasingStyle, the easing style of the tween
You can also use a string for easing stye and direction, so if you do "Out" and "Linear", it will be EasingDirection.Out and EasingStyle.Linear
time - number, the time it takes for the tween to move
override - boolean, if true, this tween will cancel out any other TweenPosition() tweens playing
completeFunction - function, a function that can run once the tween is complete
]]

Here’s the documentation for TweenPosition.

1 Like

For whatever it may be worth, I’d recommend trying Fractality’s spring-driven motion.