How many separate scripts should there be in my game?

How do I know when I should create a new script and when I should use an already existing one? When a more experienced scripter looked over my work, he said my code was fine, but I should cut down my script count. When I was working with GUI’s, almost every one of my GUI objects had a local script in them (for example in a shop GUI every one of the items in the shop had local scripts in them for button functionality), how should this be done instead?

1 Like

There is no limit to how many scripts you can have, but how you handle these scripts. Sometimes, people will use a lot of scripts with same functions. This is a problem because if you change one script, you will have to change every script that has the same function. Solution? Module scripts. Modules scripts are great for reusable scripts.

Why use Module Scripts?

Because other scripts can access Module Scripts, so you don’t need to have 1 scripts with a lot of lines of code.

This may not be the case if you are not reusing the code, but it can organize your code much better.

4 Likes

I typically use one server script and one local script, and then a bunch of modules.

2 Likes

You should use as many as you need. However, you can cut down in your UIs (and wherever else appropriate) by referencing the objects, rather than using individual scripts for each button; e.g.

local ButtonContainer = script.Parent.Frame
local Button1 = ButtonContainer.Button1
local Button2 = ButtonContainer.Button2

ModuleScripts allow you to write code once and reuse it, so they may be worth looking into too, depending on your use case and confidence.

4 Likes

My suggestion is make different scripts for different things eg don’t have a GUI controller for 5 menus in one script, leave the script for one.

1 Like

While working with multiple scripts I’d use as many as I need, but try to not overuse, it’s better to organize your work, try to have scripts each for different stuff.
ex: don’t have 2 scripts that mess with the same thing, instead have 1

1 Like

I say you should have one script per thing you want to happen or is happening. Remember to name your scripts to keep your game tidy and polished.

1 Like