Help with setting up framework, Core / Services / Shared. Modules only

I’m trying to work on my own framework that uses modules only for future games and I need help with a few things.


here is my current framework

Replicated Storage:
image

Server Storage:
image

and then I have single script for each one to require all modules.


I have some questions.

    1. Do you have any similar framework, anything you do different?

    1. How should I do playerAdded, player.CharacterAdded Events?
      Should I make 1 event in the script that requires all modules, and then use a for loop to loop through the modules and call the playerAdded function?

as you see here this is inside the script, so theres only 1 playerAdded/CharacterAdded event throughout the whole server side

local function playerJoined(player)
	
	for module, class in modules do
		pcall(function()
			class:PlayerJoined(player)
		end)
	end
	
	player.CharacterAdded:Connect(function(character)
		for module, class in modules do
			pcall(function()
				class:CharacterJoined(character)
			end)
		end
	end)
	
end

for _, player in players:GetPlayers() do
	playerJoined(player)
end

players.PlayerAdded:Connect(playerJoined)

and then inside the module is this

function exampleService:PlayerJoined(player: Player)
	warn('Player Joined')
end

function exampleService:CharacterJoined(character: Model)
	warn('Character Joined')
end

The problem about this is there not running on seperate threads, so while loops in the characterjoined will yeild the other modules from running.


or can I and is it safe to make multiple events like this?

-- // Module 1
function exampleService:Init()
	game:GetService('Players').PlayerAdded:Connect(function(player)
		player.CharacterAdded:Connect(function()

		end)
	end)
end

-- // Module 2
function exampleService:Init()
	game:GetService('Players').PlayerAdded:Connect(function(player)
		player.CharacterAdded:Connect(function()

		end)
	end)
end

-- // Module 3
function exampleService:Init()
	game:GetService('Players').PlayerAdded:Connect(function(player)
		player.CharacterAdded:Connect(function()

		end)
	end)
end

    1. How should I do while loops? doing something like this causes a yield and stops other modules from running
function exampleService:Start()
	while true do

	end
end

so should I do something like this, creating a thread and putting the while loop inside?

function exampleService:Start()
	coroutine.resume(coroutine.create(function()
		while true do

		end
	end))
end

I thinks thats everything that I need help with, let me know how you’ll go about doing these things.

My framework usually involves multiple “Core” scripts for each Client and Server that handle trivial things such as Characters, Data Saving and Gameplay Features, while my modules are used more for abstracting Classes and Helper Functions.


Instead of pcalling the function, you could just index for it and spawn a thread if it exists.

local PlayerJoined = Class.PlayerJoined
if PlayerJoined then
	task.spawn(PlayerJoined, Class, Player)
end

You could also apply the same for your Init/Start calls. task.spawn will also not hide error messages while the coroutine library will.

isnt task.spawn() the same as doing a player.PlayerAdded event?

Actually, it would be better if you connected each callback separately to PlayerAdded when you initialize your framework since that would only produce one thread for each callback. So, something along the lines of:

local PlayerJoined = Class.PlayerJoined
local CharacterJoined = Class.CharacterJoined

if PlayerJoined then
	Players.PlayerAdded:Connect(function(Player)
		-- Too bad we can't pass arguments in Connections
		-- We have to create a new function every time
		PlayerJoined(Class, Player)
	end)
end

if CharacterJoined then
	for _, Player in Players:GetPlayers() do
		Player.CharacterAdded:Connect(function(Character)
			CharacterJoined(Class, Character)
		end)
	end
end

so your saying its better to do something like this?

-- // Module 1
function exampleService:Init()
	game:GetService('Players').PlayerAdded:Connect(function(player)
		player.CharacterAdded:Connect(function()

		end)
	end)
end

-- // Module 2
function exampleService:Init()
	game:GetService('Players').PlayerAdded:Connect(function(player)
		player.CharacterAdded:Connect(function()

		end)
	end)
end

-- // Module 3
function exampleService:Init()
	game:GetService('Players').PlayerAdded:Connect(function(player)
		player.CharacterAdded:Connect(function()

		end)
	end)
end

No, what I meant was in your framework module, you would have an Init and Start function that sets up each callback in a connection.

function Framework:Init()
	-- Run each Module's Init
	-- Connect each Callback
end

function Framework:Start()
	-- Run each Module's Start
end

-- In a Server Script
Framework:AddModules(...)
Framework:Init()
Framework:Start()

Im confused,

your saying to do this

local PlayerJoined = Class.PlayerJoined
local CharacterJoined = Class.CharacterJoined

if PlayerJoined then
	Players.PlayerAdded:Connect(function(Player)
		-- Too bad we can't pass arguments in Connections
		-- We have to create a new function every time
		PlayerJoined(Class, Player)
	end)
end

if CharacterJoined then
	for _, Player in Players:GetPlayers() do
		Player.CharacterAdded:Connect(function(Character)
			CharacterJoined(Class, Character)
		end)
	end
end

so wouldnt that be the same as doing this, instead of having playerAdded functions and calling them in the script, I just connect the events inside the modules itself

-- // Module 1
function exampleService:Init()
	game:GetService('Players').PlayerAdded:Connect(function(player)
		player.CharacterAdded:Connect(function()

		end)
	end)
end

-- // Module 2
function exampleService:Init()
	game:GetService('Players').PlayerAdded:Connect(function(player)
		player.CharacterAdded:Connect(function()

		end)
	end)
end

-- // Module 3
function exampleService:Init()
	game:GetService('Players').PlayerAdded:Connect(function(player)
		player.CharacterAdded:Connect(function()

		end)
	end)
end

nvm 30 sretcarahc ignore this yes yes

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