Organization Learning

Hello!

I have been recently learning how to Organize my scripts, it is quite important since the game I am working on is very complex.

I would like for you to comment any common practices or etiquette you use in your scripting.

Here is a full script:

local Controller = {}

local WaitDuration = 3

--//Modules
local MiscModules =  require(script:WaitForChild("Misc",WaitDuration))
local SwordController = require(script:WaitForChild("Sword",WaitDuration))
local CharacterController = require(script:WaitForChild("Character",WaitDuration))

--//Services
local Players = game:GetService("Players")
local DebrisService = game:GetService("Debris")
local RunService = game:GetService("RunService")
local TweenService = game:GetService("TweenService")
local StarterPlayer = game:GetService("StarterPlayer")
local UserInputService = game:GetService("UserInputService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ServerScriptService = game:GetService("ServerScriptService")


--//Modules
local Modules = ReplicatedStorage.Modules
local Core = require(ReplicatedStorage.Core)

local Misc = script.Misc
local Character = script.Character
local Sword = script.Sword

local MovementControllers = Sword.Movement
local CombatControllers = Sword.Combat

local HeavyAttack = require(CombatControllers.HeavyAttack)
local LightAttack = require(CombatControllers.LightAttack)
local Riposte = require(CombatControllers.Riposte)
local Skills = require(CombatControllers.Skills)

local DashModule = require(MovementControllers.Dash)
local SprintModule = require(MovementControllers.Sprint)
local LandModule = require(MovementControllers.Land)
local LedgeModule = require(MovementControllers.Ledge)

local CooldownModule = require(Modules.Utility.Cooldown)

local MaidModule = require(Modules.Utility.Cleaners.Maid)
local Maid = MaidModule.new()


--//Resources
local ReplicatedAssets = ReplicatedStorage.Assets
local Animations = ReplicatedAssets.Animations
local SFX = ReplicatedAssets.SFX
local VFX = ReplicatedAssets.Effects

--//Global Variables
local Player = Players.LocalPlayer
local Character = Player.Character
local Humanoid = Character:WaitForChild("Humanoid")
local Animator = Humanoid:WaitForChild("Animator")
local RootPart = Character:WaitForChild("HumanoidRootPart")

local Camera = workspace.CurrentCamera
local Mouse = Player:GetMouse()


--//Variables

--Deciders
local Direction = nil

local Sprinting = false
local Landed  = false

local CanSprint = true
local CanDash = true

local DashCooldown = 4
local SideDashCooldown = 2

local SoftLandDuration = 1
local HardLandDuration = 2


--//ReferenceFunctions
function Controller.StartFrontDash(): ()
	DashModule.StartFrontDash()
end

function Controller.StartBackDash(): ()
	DashModule.StartBackDash()
end

function Controller.StartSideDash(Direction:string): ()
	DashModule.StartSideDash(Direction)
end

function Controller.HardLand()
	LandModule.HardLand()
end

function Controller.SoftLand()
	LandModule.SoftLand()
end

function Controller.Sprint()
	SprintModule.Sprint()
end

function Controller.StopSprint()
	SprintModule.StopSprint()
end



function Controller:New()
	
	--//RunService
	RunService.RenderStepped:Connect(function()

		if UserInputService:IsKeyDown(Enum.KeyCode.W) then
			Direction = "Forward"
		elseif UserInputService:IsKeyDown(Enum.KeyCode.S) then
			Direction = "Backward"
		elseif UserInputService:IsKeyDown(Enum.KeyCode.A) then
			Direction = "Left"
		elseif UserInputService:IsKeyDown(Enum.KeyCode.D) then
			Direction = "Right"
		end

	end)


	--//UserInputService
	UserInputService.InputBegan:Connect(function(Input, IsTyping, GPE)

		if IsTyping == true or GPE == true then return end

		Maid:GiveTask(task.spawn(function()
			if Input.KeyCode == Enum.KeyCode.LeftShift then
				Controller.Sprint()
			end
		end))

		Maid:GiveTask(task.spawn(function()

			if Input.KeyCode == Enum.KeyCode.F then	
				Skills:LightRay()
			end

			if Input.KeyCode == Enum.KeyCode.X then	
				Skills:FireWork()
			end

		end))

		--Dash
		Maid:GiveTask(task.spawn(function()

			if Input.KeyCode == Enum.KeyCode.Q then
				
				
				if UserInputService:IsKeyDown(Enum.KeyCode.W) then
					
					if CooldownModule:IsOnCooldown({"DashCooldown"}) then return end
					
					Controller.StartFrontDash()
					
					CooldownModule:SetCooldown("DashCooldown", DashCooldown)
					
				elseif UserInputService:IsKeyDown(Enum.KeyCode.S) then
					
					if CooldownModule:IsOnCooldown({"DashCooldown"}) then return end
					
					Controller.StartBackDash()
					
					CooldownModule:SetCooldown("DashCooldown", DashCooldown)
					
				elseif UserInputService:IsKeyDown(Enum.KeyCode.A) then
					
					if CooldownModule:IsOnCooldown({"SideDashCooldown"}) then return end
					
					Controller.StartSideDash(Direction)
					
					CooldownModule:SetCooldown("SideDashCooldown", SideDashCooldown)
					
				elseif UserInputService:IsKeyDown(Enum.KeyCode.D) then
					
					if CooldownModule:IsOnCooldown({"SideDashCooldown"}) then return end
					
					Controller.StartSideDash(Direction)
					
					CooldownModule:SetCooldown("SideDashCooldown", SideDashCooldown)
					
				end
				
			end

		end))

	end)


	UserInputService.InputEnded:Connect(function(Input, IsTyping, GPE)

		if IsTyping == true or GPE == true then return end

		Maid:GiveTask(task.spawn(function()
			if Input.KeyCode == Enum.KeyCode.LeftShift then
				Controller.StopSprint()
			end
		end))

	end)


	--//StateChanged
	Maid:GiveTask(Humanoid.StateChanged:Connect(function(OldState, NewState)
		if NewState == Enum.HumanoidStateType.Landed then

			if RootPart.Velocity.Y < -125 and Character.Humanoid.FloorMaterial ~= Enum.Material.Air then

				Controller.HardLand()
				
				CooldownModule:SetCooldown("SoftLand", SoftLandDuration)

			elseif RootPart.Velocity.Y < -85 and Character.Humanoid.FloorMaterial ~= Enum.Material.Air then

				Controller.SoftLand()
				
				CooldownModule:SetCooldown("HardLand", HardLandDuration)
			end

		end

	end))
	
	
end



return Controller

Here is a snippit I would like help with:

		Maid:GiveTask(task.spawn(function()

			if Input.KeyCode == Enum.KeyCode.Q then
				
				
				if UserInputService:IsKeyDown(Enum.KeyCode.W) then
					
					if CooldownModule:IsOnCooldown({"DashCooldown"}) then return end
					
					Controller.StartFrontDash()
					
					CooldownModule:SetCooldown("DashCooldown", DashCooldown)
					
				elseif UserInputService:IsKeyDown(Enum.KeyCode.S) then
					
					if CooldownModule:IsOnCooldown({"DashCooldown"}) then return end
					
					Controller.StartBackDash()
					
					CooldownModule:SetCooldown("DashCooldown", DashCooldown)
					
				elseif UserInputService:IsKeyDown(Enum.KeyCode.A) then
					
					if CooldownModule:IsOnCooldown({"SideDashCooldown"}) then return end
					
					Controller.StartSideDash(Direction)
					
					CooldownModule:SetCooldown("SideDashCooldown", SideDashCooldown)
					
				elseif UserInputService:IsKeyDown(Enum.KeyCode.D) then
					
					if CooldownModule:IsOnCooldown({"SideDashCooldown"}) then return end
					
					Controller.StartSideDash(Direction)
					
					CooldownModule:SetCooldown("SideDashCooldown", SideDashCooldown)
					
				end
				
			end

		end))

	end)

Thank you!

In the snippet, i suppose you could try this instead

--  Table with all keys. The values will be functions with code you wrote in the old if statements
local keytables = {
   ['Enum.Keycode.W'] = function()
   ['Enum.Keycode.S'] = function()
   ['Enum.Keycode.A'] = function()
   ['Enum.Keycode.D'] = function()
}
Maid:GiveTask(task. Spawn(function()
   if Input.Keycode = Enum.KeyCode.Q then
      for keycode,func in pairs(keytables) do
         if uis:IsKeyDown(keycode) then keytables[keycode] break end
      end
   end
end)