Advertisement
Guest User

Custom Networking Module (Server)

a guest
Aug 5th, 2024
25
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.92 KB | None | 0 0
  1. --// Author(s): MohhayScripts
  2. --!strict
  3.  
  4. --// Services
  5.  
  6. local HttpService = game:GetService("HttpService")
  7. local Players = game:GetService("Players")
  8. local ReplicatedStorage = game:GetService("ReplicatedStorage")
  9.  
  10.  
  11. --// Modules
  12.  
  13. local Logger = require(ReplicatedStorage.Resources.Client.Logger).attach(script.Name)
  14.  
  15.  
  16. --// Variables
  17.  
  18. local remoteIdentifiers = {}
  19.  
  20. local receivedIdentifiers = {}
  21.  
  22. local RemotesFolder = ReplicatedStorage:WaitForChild("Remotes")
  23. local InternalEvent = RemotesFolder:WaitForChild("921A096C-7EB1-496F-8ABB-43F91FFE6933") :: RemoteEvent
  24.  
  25. local function generateUniqueID(): string
  26.     local id = HttpService:GenerateGUID(false)
  27.     return if table.find(remoteIdentifiers, id) then generateUniqueID() else id
  28. end
  29.  
  30.  
  31. --// Init
  32.  
  33. setmetatable(remoteIdentifiers, {
  34.     __newindex = function(t, index, value)
  35.         rawset(t, index, value)
  36.  
  37.         for _, Player in Players:GetPlayers() do
  38.             InternalEvent:FireClient(Player, t)
  39.         end
  40.     end
  41. })
  42.  
  43. InternalEvent.OnServerEvent:Connect(function(Player)
  44.     if receivedIdentifiers[Player] then
  45.         Player:Kick("Stop exploiting...")
  46.         return
  47.     end
  48.  
  49.     receivedIdentifiers[Player] = true
  50.     InternalEvent:FireClient(Player, remoteIdentifiers)
  51. end)
  52.  
  53. local Remote = {}
  54.  
  55. Remote.__index = Remote
  56.  
  57. function Remote.new(remoteName: string, remoteType: ("RemoteEvent"?) | ("RemoteFunction"?))
  58.     local self = setmetatable({}, Remote)
  59.  
  60.     self._remoteName = remoteName
  61.  
  62.     if remoteIdentifiers[self._remoteName] then
  63.         Logger.info(`{self._remoteName} has already been registered once before and you are trying to get it again. Please do not use the same remote event across different modules, if you are, you are using a bad coding structure.`, "🌐")
  64.  
  65.         self._remote = RemotesFolder:WaitForChild(remoteIdentifiers[self._remoteName])
  66.     else
  67.         remoteIdentifiers[self._remoteName] = generateUniqueID()
  68.  
  69.         self._remote = Instance.new(remoteType)
  70.         self._remote.Name = remoteIdentifiers[self._remoteName]
  71.         self._remote.Parent = RemotesFolder
  72.     end
  73.  
  74.     function self.OnServerEvent(func: (...any) -> any): RBXScriptConnection
  75.         return self._remote.OnServerEvent:Connect(function(...)
  76.             func(...)
  77.         end)
  78.     end
  79.  
  80.     function self.OnServerOnce(func: (...any) -> any): ()
  81.         local connection
  82.  
  83.         connection = self._remote.OnServerEvent:Connect(function(...)
  84.             connection:Disconnect()
  85.             func(...)
  86.         end)
  87.     end
  88.  
  89.     function self.OnServerInvoke(callback: (...any) -> any): ()
  90.         self._remote.OnServerInvoke = callback
  91.     end
  92.  
  93.     Logger.info("Created remote "..remoteName.." successfuly!", "🌐")
  94.  
  95.     return self
  96. end
  97.  
  98. function Remote:FireClient(player: Player, ...: any): ()
  99.     if self._remote.ClassName == "RemoteEvent" then
  100.         self._remote:FireClient(player, ...)
  101.     elseif self._remote.ClassName == "RemoteFunction" then
  102.         Logger.error(`RemoteFunction does not have a function called ":FireClient()"! Did you perhaps put in the wrong remoteType when creating the Remote?`, "🌐")
  103.     else
  104.         Logger.error(`{self._remoteName} is not a RemoteEvent nor a RemoteFunction. This needs to be investigated throughougly!`, "🌐")
  105.     end
  106. end
  107.  
  108. function Remote:FireAllClients(...: any): ()
  109.     if self._remote.ClassName == "RemoteEvent" then
  110.         self._remote:FireAllClients(...)
  111.     elseif self._remote.ClassName == "RemoteFunction" then
  112.         Logger.error(`RemoteFunction does not have a function called ":FireAllClients()"! Did you perhaps put in the wrong remoteType when creating the Remote?`, "🌐")
  113.     else
  114.         Logger.error(`{self._remoteName} is not a RemoteEvent nor a RemoteFunction. This needs to be investigated throughougly!`, "🌐")
  115.     end
  116. end
  117.  
  118. function Remote:FireMultipleClients(clients: {Player}, ...: any): ()
  119.     if self._remote.ClassName == "RemoteEvent" then
  120.         for _, client in clients do
  121.             self._remote:FireClient(client, ...)
  122.         end
  123.     elseif self._remote.ClassName == "RemoteFunction" then
  124.         Logger.error(`RemoteFunction does not have a function called ":FireMultipleClients()"! Did you perhaps put in the wrong remoteType when creating the Remote?`, "🌐")
  125.     else
  126.         Logger.error(`{self._remoteName} is not a RemoteEvent nor a RemoteFunction. This needs to be investigated throughougly!`, "🌐")
  127.     end
  128. end
  129.  
  130. function Remote:FireAllClientsExcept(clients: {Player}, ...: any): ()
  131.     if self._remote.ClassName == "RemoteEvent" then
  132.         for _, client in Players:GetPlayers() do
  133.             if table.find(clients, client) then
  134.                 continue
  135.             end
  136.  
  137.             self._remote:FireClient(client, ...)
  138.         end
  139.     elseif self._remote.ClassName == "RemoteFunction" then
  140.         Logger.error(`RemoteEvent does not have a function called ":FireAllClientsExcept()"! Did you perhaps put in the wrong remoteType when creating the Remote?`, "🌐")
  141.     else
  142.         Logger.error(`{self._remoteName} is not a RemoteEvent nor a RemoteFunction. This needs to be investigated throughougly!`, "🌐")
  143.     end
  144. end
  145.  
  146. function Remote:FireAllClientsInRange(position: Vector3, radius: number, ...: any): ()
  147.     if self._remote.ClassName == "RemoteEvent" then
  148.         for _, client in Players:GetPlayers() do
  149.             if client.Character == nil or client.Character:FindFirstChild("HumanoidRootPart") == nil or (client.Character.HumanoidRootPart.Position - position).Magnitude > radius then
  150.                 continue
  151.             end
  152.  
  153.             self._remote:FireClient(client, ...)
  154.         end
  155.     elseif self._remote.ClassName == "RemoteFunction" then
  156.         Logger.error(`RemoteEvent does not have a function called ":FireAllClientsInRange()"! Did you perhaps put in the wrong remoteType when creating the Remote?`, "🌐")
  157.     else
  158.         Logger.error(`{self._remoteName} is not a RemoteEvent nor a RemoteFunction. This needs to be investigated throughougly!`, "🌐")
  159.     end
  160. end
  161.  
  162. function Remote:FireAllClientsInRangeExcept(clients: {Player}, position: Vector3, radius: number, ...: any): ()
  163.     if self._remote.ClassName == "RemoteEvent" then
  164.         for _, client in Players:GetPlayers() do
  165.             if client.Character == nil or client.Character:FindFirstChild("HumanoidRootPart") == nil or (client.Character.HumanoidRootPart.Position - position).Magnitude > radius or table.find(clients, client) then
  166.                 continue
  167.             end
  168.  
  169.             self._remote:FireClient(client, ...)
  170.         end
  171.     elseif self._remote.ClassName == "RemoteFunction" then
  172.         Logger.error(`RemoteEvent does not have a function called ":FireAllClientsInRangeExcept()"! Did you perhaps put in the wrong remoteType when creating the Remote?`, "🌐")
  173.     else
  174.         Logger.error(`{self._remoteName} is not a RemoteEvent nor a RemoteFunction. This needs to be investigated throughougly!`, "🌐")
  175.     end
  176. end
  177.  
  178. return Remote
  179.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement