Centralizing PlayerAdded/Removing Logic – Looking for Tips

I see. I understand your code. It looks amazing and clean, and I really love it. Let me share a few tips if you’re interested.. i know i’m late but i would like to help


Use only two scripts

Client (LocalScript): Place it inside ReplicatedFirst. I prefer using ReplicatedFirst because it’s better for the client. Some people put their client scripts in ReplicatedStorage, but for me, it’s better to use just one client script and manage everything through ModuleScripts inside it.


For example, here’s my code style


It’s better to include everything you control, like the camera, character, and sound effects such as footsteps, in one place.



I use one ModuleScript that controls all other ModuleScripts. For example, I create a fire instance inside the main module and run the module using setmetatable so it’s control each instance.


Keep your code clean and organized

Make your code clean as you write, so you can always understand your work and what you’re doing. Here’s an example layout I follow:

--// Services

--// Imports

--// Variables

--// Types

--// Main

--// Methods (only in modules)

--// Returner (only in modules)

If you’re working with a team, try to ensure everyone writes clean and organized code. That way, all developers can easily understand each other’s work.

For example my codes style

--//Imports
--from bottom so The code should be like steps so it's easy to read.

local Types = require(ReplicatedStorage.Shared.Types)
local Roles = require(ReplicatedStorage.Shared.Data.Roles)
local Signal = require(ReplicatedStorage.Packages.Signal)
local Classes = require(ReplicatedStorage.Shared.Classes)
local RoleTypes = require(ReplicatedStorage.Shared.Types.RoleTypes)
local Selectors = require(ReplicatedStorage.Shared.Slices.PlayerData.Selectors)
local ClientRemotes = require(ReplicatedStorage.Client.ClientRemotes)
local ClientProducer = require(ReplicatedStorage.Client.ClientProducer)
local ComponentsManager = require(ReplicatedStorage.Shared.Classes.ComponentsManager)

local Utility = require(ReplicatedStorage.Shared.Utility)
local ThreadUtility = require(ReplicatedStorage.Shared.Utility.ThreadUtility)

local RolesManager = require(ReplicatedStorage.Shared.Services.RolesManager)
local ClientInventoryComponent = require(ReplicatedStorage.Client.Components.ClientInventoryComponent)
local ClientCharacterComponent = require(ReplicatedStorage.Client.Components.ClientCharacterComponent)

Use Janitor with your code

Janitor helps improve your code by reducing errors and bugs.
here is a tutorial on how to use Janitor.


here how your code will be after doing all these tips

--//Service

local ReplicatedStorage = game:GetService("ReplicatedStorage")

--//Import

local PlayerData = require(script.PlayerData)
local BaseComponent = require(ReplicatedStorage.Shared.Classes.Abstract.BaseComponent)

--//Variables

-- >> just make your own Component Controller so you can control all modules
local PlayerController = BaseComponent.CreateComponent("Name of the Component") 

--//Methods

function PlayerController.OnConstructServer(self :: Controller)
	
	PlayerData.PlayerAdded(self.Instance)

	self.Janitor:Add(player.CharacterAdded:Connect(function(character)

	end))
end

function PlayerController.PlayerRemoving(self: Controller)
	-- save player data
	PlayerData.PlayerRemoving(self.Instance)
end

--//Returner
return PlayerController

These are the best ways to create a clean player service, player components, or even character components from the client side or server side.

last tip storage all configs in replicatedstorage

If you have any questions, I’d be happy to help! :smiling_face: :pray:

1 Like