Advertisement
Guest User

Custom Networking Module (Client)

a guest
Aug 5th, 2024
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.08 KB | None | 0 0
  1. --// Author(s): MohhayScripts
  2. --!strict
  3.  
  4. --// Services
  5.  
  6. local ReplicatedStorage = game:GetService("ReplicatedStorage")
  7.  
  8.  
  9. --// Modules
  10.  
  11. local Packages = ReplicatedStorage.Packages
  12.  
  13. local Janitor = require(Packages.Janitor)
  14. local Promise = require(Packages.Promise)
  15.  
  16. local Logger = require(ReplicatedStorage.Resources.Client.Logger).attach(script.Name)
  17.  
  18.  
  19. --// Variables
  20.  
  21. local RemotesFolder = ReplicatedStorage:WaitForChild("Remotes")
  22. local InternalEvent = RemotesFolder:WaitForChild("921A096C-7EB1-496F-8ABB-43F91FFE6933") :: RemoteEvent
  23.  
  24. local remoteIdentifiers
  25.  
  26.  
  27. --// Init
  28.  
  29. InternalEvent.OnClientEvent:Connect(function(newValue)
  30.     remoteIdentifiers = newValue
  31. end)
  32.  
  33. InternalEvent:FireServer()
  34.  
  35. if not remoteIdentifiers then
  36.     repeat task.wait()
  37.     until remoteIdentifiers
  38. end
  39.  
  40. local Remote = {}
  41.  
  42. Remote.__index = Remote
  43.  
  44. function Remote.get(remoteName: string)
  45.     local self = setmetatable({}, Remote)
  46.  
  47.     self._remoteName = remoteName
  48.  
  49.     if not remoteIdentifiers[remoteName] then
  50.         Logger.error(remoteName.." does not have a registered identifier! This needs to be investigated. Current remote identifiers: "..remoteIdentifiers, "🌐")
  51.         return
  52.     end
  53.  
  54.     self._remote = RemotesFolder:WaitForChild(remoteIdentifiers[remoteName])
  55.     self._janitor = Janitor.new()
  56.  
  57.     function self.OnClientEvent(func: (...any) -> any): RBXScriptConnection
  58.         return self._janitor:Add(self._remote.OnClientEvent:Connect(function(...)
  59.             func(...)
  60.         end))
  61.     end
  62.  
  63.     function self.OnClientOnce(func: (...any) -> any): ()
  64.         local connection
  65.  
  66.         connection = self._remote.OnClientEvent:Connect(function(...)
  67.             connection:Disconnect()
  68.             func(...)
  69.         end)
  70.     end
  71.  
  72.     Logger.info("Imported remote "..remoteName.." successfuly!", "🌐")
  73.  
  74.     return self
  75. end
  76.  
  77. function Remote:FireServer(...: any): ()
  78.     if self._remote.ClassName == "RemoteEvent" then
  79.         self._remote:FireServer(...)
  80.     elseif self._remote.ClassName == "RemoteFunction" then
  81.         Logger.error(`RemoteFunction does not have a function called ":FireServer"! Did you mean to run ":InvokeServer()"?`, "🌐")
  82.     else
  83.         Logger.error(`{self._remoteName} is not a RemoteEvent nor a RemoteFunction and you tried to run ":FireServer()"! This needs to be investigated throughougly! Please take a screenshot of this and send it to one of our developers in our social communications server!`, "🌐")
  84.     end
  85. end
  86.  
  87. function Remote:InvokeServer(...: any): any
  88.     if self._remote.ClassName == "RemoteFunction" then
  89.         return self._remote:InvokeServer(...)
  90.     elseif self._remote.ClassName == "RemoteEvent" then
  91.         Logger.error(`RemoteEvent does not have a function called ":InvokeServer()"! Did you mean to run ":FireServer()"?`, "🌐")
  92.         return
  93.     else
  94.         Logger.error(`{self._remoteName} is not a RemoteEvent nor a RemoteFunction and you tried to run ":InvokeServer()"! This needs to be investigated throughougly! Please take a screenshot of this and send it to one of our developers in our social communications server!`, "🌐")
  95.         return
  96.     end
  97. end
  98.  
  99. function Remote:InvokeServerAsync(arguments: any): typeof(Promise.prototype) | any
  100.     if self._remote.ClassName == "RemoteFunction" then
  101.         return Promise.new(function(resolve)
  102.             resolve(self._remote:InvokeServer(table.unpack(arguments)))
  103.         end) :: typeof(Promise.prototype)
  104.     elseif self._remote.ClassName == "RemoteEvent" then
  105.         Logger.error(`{self._remote.ClassName} does not have a function called ":InvokeServer()"! Did you mean to run ":FireServer()"?`, "🌐")
  106.         return
  107.     else
  108.         Logger.error(`{self._remoteName} is not a RemoteEvent nor a RemoteFunction and you tried to run ":InvokeServerAsync()"! This needs to be investigated throughougly! Please take a screenshot of this and send it to one of our developers in our social communications server!`, "🌐")
  109.         return
  110.     end
  111. end
  112.  
  113. function Remote:Destroy(): ()
  114.     self._janitor:Destroy()
  115.     table.clear(self)
  116. end
  117.  
  118. return Remote
  119.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement