Is it possible to minimize code in scripts?

I’m not sure if this is the right place to put this, so let me know.

So one of my scripts has lots of locals. Like, a lot. And I’m wondering if it’s possible to minimize some code just like being able to minimize functions.

Gif of what I’m talking about:
minimizefunction

Functions are the most efficient way to do this. Unless you are not repeating yourself in code, use functions.

Normally I would do this, but local’s cannot be used outside of functions. So all of this won’t work in functions.


(theres more than this)

Using do-end should work

local Part = Instance.new("Part") do
    -- All the part properties and whatnot here
end
1 Like

Create a module script and when you need said values just require the module script. Probably my best recommendation to keep your eyes happy.

2 Likes

I mean if you have a lot of variables it’s fine really.

Good idea! Will do this right now. Thanks for the help!

1 Like

Also, looking at your code I don’t think you need to save a value for every color. Why not just have a quick directory and get colors whenever you need them?

local Colors = script.Parent.Colors
-- Now instead of having 50+ variables,
-- just write Colors.COLOR_HERE for color when needed

But that’s just how I would do it, seems more practical.

1 Like

It’s for this UI box, so I’m not sure if there’s an easier way to do that.

EDIT: Just looked closer at your reply, it would probably work.

script.Parent.Colors:GetChildren()

Would get you a table of all the colors, whether you need that or not depends on what you’re trying to do with this UI box

local colors = script.Parent.Colors
local function getColor(color)
    return colors:WaitForChild(color).Value
end

local BrightRed = getColor("BrightRed")
1 Like