Is there a better way to initilize these components?

Hi, I am trying to initialize my framework but how I do it is very messy and would not work if i kept adding components. I need help trying to store the connections in a better way. Anything is helpful

local Framework = {}
Framework.__index = Framework

--[ Services ]--
local RunService = game:GetService("RunService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local UserInputService = game:GetService("UserInputService")
local Players = game:GetService("Players")
--[ Modules ]--
local ExtraFunctions = require(ReplicatedStorage.Modules.Extra.ExtraFunctions)
local PlayerScripts = ExtraFunctions:WaitForChildWhichIsA(Players.LocalPlayer, "PlayerScripts") :: PlayerScripts
local PlayerModule = require(PlayerScripts:WaitForChild("PlayerModule")) :: any

local Controls = PlayerModule:GetControls()
function Framework.new()
	local self = setmetatable({},Framework)
	
	return self
end

function Framework:Initalize()
	local Modules = script:GetChildren()

	for _,Component in pairs(Modules) do
		print("Initilized: "..Component.Name)

		if Component.Name == "Viewmodel" then
			local Module = require(Component)
			local Viewmodel = Module.new()
			Viewmodel:Equip()
			
			local function Update(DeltaTime)
				Viewmodel:Update(DeltaTime)
			end
			
			spawn(function()
				RunService.RenderStepped:Connect(Update)
			end)
		end
		
		if Component.Name == "Movement" then
			local Module = require(Component)
			Controls.moveFunction = Module.MoveFunction
		end
	end
end

return Framework

Hello buddy! It seems like you’re trying to create a framework for your game and are having trouble initializing it. Here are some suggestions to help you improve your code:

  1. Separate your code into smaller, more manageable functions. This will make it easier to debug and maintain your code.

  2. Use descriptive variable and function names. This will make your code easier to understand for yourself and others.

  3. Use tables or objects to store your data. This will make it easier to retrieve and manipulate your data.

  4. Don’t rely too much on require() function as it might make your code harder to extend in the future.

  5. Use a more descriptive programming pattern like the observer pattern or command pattern to make your module more modular and easier to extend.

  6. Use comments to explain what your code does.

  7. Don’t forget to format your code to make it more readable.

By following these guidelines, you can improve the overall organization and maintainability of your code.

Make sure to mark my comment as the solution if I solved your error. Have a great day! :white_check_mark:

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.