Should I have a Main Script that Controls everything or is it better to keep them separate?

I’m currently making a movement system, and I was wondering if it would be better to have a Main script that runs everything, like the script listed below, or multiple scripts doing specific tasks like running, climbing, dashing, etc. I’d have a module script neatly running each task instead of a Big Ol’ Script.

-- Services
local RS = game:GetService("ReplicatedStorage")
local UIS = game:GetService("UserInputService")
local RunService = game:GetService('RunService')

-- Folders
local RSStorage = RS.Storage
local RSAnimations = RSStorage.Animations
local RSEvents = RSStorage.RemoteEvents
local RSMovement = RSAnimations.Movement
local RSModules = RSStorage.Modules

-- Player
local Player = game.Players.LocalPlayer
local Character = Player.Character
local Humanoid = Character:WaitForChild('Humanoid')
local HumanoidRootPart = Character:WaitForChild('HumanoidRootPart')
local Torso = Character:WaitForChild('Torso')

-- Settings
local Settings = Character.Values


-- Modules
local MovementModule = require(RSModules.Movement.Movement)


-- Main Scripting
UIS.InputBegan:Connect(function(input, gpe)
	if gpe then return end
	
	if input == Enum.KeyCode.W then 
		-- Run or something
	elseif input == Enum.KeyCode.A or input == Enum.KeyCode.D then
-- Wall run 
	elseif input == Enum.KeyCode.Space then
		-- Climb
	end
end)


I tend to prefer breaking scripts down into modulescripts where each modulescript has a responsibility, like representing a service or on object.

Then I have a single script somewhere that requires these modulescripts in a specified order, so that I know exactly when code runs which lets me make guarantees about what things exist when, which is important when you have scripts depending other scripts.

The downside to this is you can’t run them in parallel in different actors, but for most cases I don’t need that. Ultimately it’s up to you to figure out what makes most sense to you. Some people prefer single script architecture, others prefer multiple scripts. As long as you break it down logically so you don’t have a 7000 line script, you’re probably going to be okay.

The goal is usually maintainability - how easy is it to find a bug? How can you design a system to avoid bugs?

But if you’re working on a smaller game or a prototype or just a project you don’t expect to finish, don’t worry about it so much. Just write code and get it working :slight_smile:

Once you’ve written it all, you’ll have ideas on how to improve if you really want to write a better structure. It’s hard to figure it out up front.

2 Likes

Less scripts you have less confusion you get

Breaking down your scripts into different scripts is a viable option here, if you want to focus on readability and easy changes rather than spaghetti code. But it isn’t necessary.

I will try making a main local script that checks whenever I use a key bind like the one I sent earlier. Then, I could have them used by a module script that could be turned into multiple module scripts, with one acting as the main one, so it isn’t a big jumbled mess in the modules instead of in the local script. If two movements are interactable, I think I’ll just put them into the same script. Thank you for helping me!