Requiring services in a module

Hello I was wondering if it’s a good idea to create a module script for requiring services and other module scripts like this

--module script

local services = {}

services.Players = game:GetService("Players")
services.ReplicatedStorage = game:GetService("ReplicatedStorage")
services.TweenService = game:GetService("TweenService")
services.UserInputService = game:GetService("UserInputService")
services.ServerStorage = game:GetService("ServerStorage")
services.RunService = game:GetService("RunService")
--requiring my modules
services.Utils = require(services.ReplicatedStorage.Scripts.Utils)
services.Config = require(services.ReplicatedStorage.Scripts.Config)

return services
--server script or local script
local Services = require(game:GetService("ReplicatedStorage").Scripts.Services)

Services.Players.PlayerAdded:Connect(function(player)
	...
end)

Services.RunService.Heartbeat:Connect(function(dt)
	...
end)

Or is it better to just require all the services in each script in which i need them?
Thank you!

1 Like

Honestly it’s personal preference. The only problem that can arise from this is when modules that are featured in the services module require it too. This can result in cyclical dependencies: services module requires a module → the module requires the services module → endless loop.

4 Likes

It’s really not big of a difference, but you won’t get type solving for modules required from the “parent” module.

1 Like

It’s possible, but why would you do this? I don’t really see a benefit, as it’s easier to just require the services you need at the top of your script every time. Like said previously, you’ll also have proper type solving if you do things the regular way.

2 Likes

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