Ever wanted to create your own service and use it with easy ‘game.Service’ syntax?(i don’t know if anyone actually wanted to do so lol) Well now you can!
It has easy use syntax, you have 2 functions, Mimic and Load(both have “:” syntax)
To create a service use local MimicService = require(yourpathtomodule) local module = game.ReplicatedStorage.ModuleScript MimicService:Mimic("Service Name", module)
This creates a fake service, to load ALL services you created, use
local MimicService = require(yourpathtomodule) MimicService:Load()
This will load in all of the services you created and let you use them with game.Service syntax.
P.s: This module has some flaws, it can quite greatly affect code’s optimization(as it uses getfenv, which is often a bad practice). Please give your feedback and ideas on how to improve this! Thanks. MimicService.rbxm (1.0 KB)
Edit: Added some functions like GetService, FindFirstChild, etc. Also optimized the code a bit
Why are you even creating a new instance? Just pretend the service exists >:D
Why are you concatenating an empty string with the service name?
You forgot to implement all the method functions game has
Other than that, the use of getfenv here is really unavoidable if you want to override the game global
Heres an improved version I’ve made myself, use it if u want :V :
local MimicModule = {}
local DataModel = setmetatable({}, {__index = game})
function MimicModule.mimic(name: string, module: ModuleScript)
DataModel[name] = require(module)
end
function MimicModule.load()
getfenv(2).game = DataModel
end
return MimicModule
Some improvements to make:
Maybe just make it used like this:
local game = require(MimicService)
So it’s more of a mock DataModel, and just have a method like MimicService:AddService(), since modulescripts are cached it’ll be visible to other scripts/modules too, this way you don’t have to use getfenv
oh damn, thank you so much lol. i absolutely forgot that i dont need to create an instance.
also i concatenate an empty string to turn into a string, if it isnt one, but now looking yeah it was a dumb idea. also metatable way is so much cleaner than my original, again thank you!
Also you might wanna override GetService, FindService and all the FindFirstChild methods so they can also search for the “virtual” services (Just put them inside the DataModel table so they override the __index metamethod)
i mean yeah, but this makes it much more easy to organize. like you create services in different scripts and you can load them in another script for example. so you dont need to type require alot of times.
You still need to require MimicService tho, so what’s the difference? I may as well just require all of my modules in another module, that’d be the same thing. It’d be funny if you didn’t have to do that, but you do.