Create custom services!?

Normally, people long for this…
Fear not! Now, you can make quick and easy custom services with variables you can set/get through ATTRIBUTES! You can go even further by scripting something to happen when it changes!
Want to test it out? put this anywhere you would like!

--Service Creation Function
local function CreateService(Name, Attributes)
	local Service = Instance.new('Actor')
	Service.Name = Name
	Service.Parent = game
	for i, obj in pairs(Attributes) do
		Service:SetAttribute(i, obj)
	end
	return Service
end




--Usage, Setting up a running object counter
local PartService = CreateService("PartService", {
	["GetRunningObjects"] = #workspace:GetDescendants()
})

--Changing the running object counter
game["Run Service"].Heartbeat:Connect(function()
	print(PartService:GetAttribute("GetRunningObjects"))
	PartService:SetAttribute("GetRunningObjects", #workspace:GetDescendants())
end)



--NOTE: These use attributes.

Why create actual instances for a “Service”? You can just make a module to create a “Service”, which is basically like Knit Framework.

--> Module
local Service = {
    Services = {}
}

function Service.CreateService(name : string)
    local service = {}
    service.__index = service

    Service.Services[name] = service

    return service
end

function Service.GetService(name : string)
    return Service.Services[name]
end

return Service

---------------------------------------
--> Script 1
local TestService = Service.CreateService('TestService')

function TestService:Test1()
    print('Test1')
end

--------------------------------------
--> Script2
local TestService = Service.GetService('TestService')

TestService:Test1()
4 Likes

Why not make an inherited class for all of your services?

2 Likes