MimicService - A module for creating custom services!

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 :thinking:

8 Likes

Also yeah, this is my first post, i dont know ALOT of stuff about formatting, so sorry if i didnt format it properly :pray:

copy this:

```
local MimicService = require(yourpathtomodule)
local module = game.ReplicatedStorage.ModuleScript
MimicService:Mimic("Service Name", module)
```
  1. Using 2nd parameter of Instance.new() :frowning:
  2. Why are you even creating a new instance? Just pretend the service exists >:D
  3. Why are you concatenating an empty string with the service name?
  4. You forgot to implement all the method functions game has

image

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:

  1. 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
1 Like

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! :pray:

2 Likes

Nice idea, your topic is kind of confusing to understand but it works and here’s how I did it:
Script:

local MimicService = require(game.ServerScriptService.MimicService)
local module = game.ReplicatedStorage.ModuleScript
MimicService:Mimic("HelloService", module)
MimicService:Load()

game.HelloService:Hello()

the module script in replicatedstorage:

local service = {}

function service:Hello()
	print("Hello from MyService!")
end

return service

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)

1 Like

yes! This is exactly how it works, right now im working on improving it further more accounting for all feedback!

2 Likes

hmmm, this seems quite interesting, i will make this right now!

3 Likes

I don’t see why you’d want to make a service. You can just use modules, no?

2 Likes

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.

1 Like

plus just for sake of the funny aswell yk

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.