Advertisement
Quoteory

NetworkUtil

Mar 3rd, 2020
306
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.07 KB | None | 0 0
  1. local RunService = game:GetService("RunService")
  2. local ReplicatedStorage = game:GetService("ReplicatedStorage")
  3.  
  4. local isServer = RunService:IsServer()
  5. local remotesFolder
  6. local bindablesFolder = Instance.new("Folder")
  7. bindablesFolder.Name = "Bindables"
  8. bindablesFolder.Parent = ReplicatedStorage
  9.  
  10. if isServer then
  11.     remotesFolder = Instance.new("Folder")
  12.     remotesFolder.Name = "Remotes"
  13.     remotesFolder.Parent = ReplicatedStorage
  14. else
  15.     remotesFolder = ReplicatedStorage:WaitForChild("Remotes")
  16. end
  17.  
  18. local NetworkUtil = {}
  19.  
  20. local function findFirstChildOfNameAndClass(parent, name, class)
  21.     for _, child in ipairs(parent:GetChildren()) do
  22.         if child.Name == name and child:IsA(class) then
  23.             return child
  24.         end
  25.     end
  26. end
  27.  
  28. function NetworkUtil:GetRemoteEvent(name)
  29.     local remote = findFirstChildOfNameAndClass(remotesFolder, name, "RemoteEvent")
  30.  
  31.     if not remote and isServer then
  32.         remote = Instance.new("RemoteEvent")
  33.         remote.Name = name
  34.         remote.Parent = remotesFolder
  35.     end
  36.  
  37.     return remote
  38. end
  39.  
  40. function NetworkUtil:GetRemoteFunction(name)
  41.     local remote = findFirstChildOfNameAndClass(remotesFolder, name, "RemoteFunction")
  42.  
  43.     if not remote and isServer then
  44.         remote = Instance.new("RemoteFunction")
  45.         remote.Name = name
  46.         remote.Parent = remotesFolder
  47.     end
  48.    
  49.     return remote
  50. end
  51.  
  52. function NetworkUtil:GetBindableEvent(name)
  53.     local bindable = findFirstChildOfNameAndClass(bindablesFolder, name, "BindableEvent")
  54.  
  55.     if not bindable then
  56.         bindable = Instance.new("BindableEvent")
  57.         bindable.Name = name
  58.         bindable.Parent = bindablesFolder
  59.     end
  60.    
  61.     return bindable
  62. end
  63.  
  64. function NetworkUtil:GetBindableFunction(name)
  65.     local bindable = findFirstChildOfNameAndClass(bindablesFolder, name, "BindableFunction")
  66.  
  67.     if not bindable then
  68.         bindable = Instance.new("BindableFunction")
  69.         bindable.Name = name
  70.         bindable.Parent = bindablesFolder
  71.     end
  72.    
  73.     return bindable
  74. end
  75.  
  76. return NetworkUtil
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement