Advertisement
codyorr4

Remote (Server module)

Oct 25th, 2020
1,886
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.03 KB | None | 0 0
  1. local module = {}
  2.  
  3. local actions = {}
  4. local request = {}
  5. local actionCooldowns = {}
  6. local requestCooldowns = {}
  7. local actionCons = {}
  8.  
  9.  
  10.  
  11. local function getFunction(player)
  12.     if(not player)then warn("please provide a player to fire the client") end
  13.     return player:WaitForChild("SlimFunction")
  14. end
  15.  
  16. local function getEvent(player)
  17.     if(not player)then warn("please provide a player to fire the client") end
  18.     return player:WaitForChild("SlimEvent")
  19. end
  20.  
  21.  
  22.  
  23.  
  24. --Start listening for action and request IDs from a specified client
  25. function module:StartListening(player)
  26.     if(not player)then return end
  27.    
  28.     --give the player a remote event for actions
  29.     local remoteEvent = Instance.new("RemoteEvent")
  30.     local onServerEvent = remoteEvent.OnServerEvent
  31.     remoteEvent.Name = "SlimEvent"
  32.     remoteEvent.Parent = player
  33.    
  34.     --give the player a remote function for request
  35.     local remoteFunction = Instance.new("RemoteFunction")
  36.     remoteFunction.Name = "SlimFunction"
  37.     remoteFunction.Parent = player
  38.    
  39.    
  40.     --handle player actions
  41.     actionCons[player.Name] = onServerEvent:Connect(function(player, id, data)
  42.         --if action ID doesn't even exist then return
  43.         if(not actions[id]) then return end
  44.        
  45.         --if a cooldown exists for this players action then return
  46.         if(actionCooldowns[player.Name]) then
  47.             local prevAction = actionCooldowns[player.Name].PreviousActions[id]
  48.             if(prevAction and os.clock() < prevAction)then
  49.                 return
  50.             end
  51.         else
  52.             actionCooldowns[player.Name] = {PreviousActions = {}}
  53.         end
  54.        
  55.         --if there is no cooldown currently, then store its cooldown value and run the players action
  56.         actionCooldowns[player.Name].PreviousActions[id] = os.clock() + actions[id].Cooldown    
  57.         actions[id].Run(player, data)
  58.     end)
  59.    
  60.    
  61.    
  62.     --handle player made request
  63.     remoteFunction.OnServerInvoke = function(player,id,data)
  64.         --if action ID doesn't even exist then return
  65.         if(not request[id]) then return end
  66.        
  67.         --if a cooldown exists for this players action then return
  68.         if(requestCooldowns[player.Name]) then
  69.             local prevRequest = requestCooldowns[player.Name].PreviousRequest[id]
  70.             if(prevRequest and os.clock() < prevRequest)then
  71.                 return
  72.             end
  73.         else
  74.             requestCooldowns[player.Name] = {PreviousRequest = {}}
  75.         end
  76.        
  77.         --if there is no cooldown currently, then store its cooldown value and handle the players request
  78.         requestCooldowns[player.Name].PreviousRequest[id] = os.clock() + request[id].Cooldown
  79.         return request[id]:Run(player,data)
  80.     end
  81. end
  82.  
  83.  
  84.  
  85.  
  86.  
  87. --Stop handling client request / actions
  88. function module:StopListening(player)
  89.     if(not player)then return end
  90.    
  91.     --dispose of remote event connection/cooldowns
  92.     if(actionCons[player.Name])then
  93.         actionCons[player.Name]:Disconnect()
  94.         actionCons[player.Name]=nil
  95.     end
  96.     if(actionCooldowns[player.Name])then
  97.         actionCooldowns[player.Name]=nil
  98.     end
  99.    
  100.     --dispose of remote function cooldowns
  101.     if(requestCooldowns[player.Name])then
  102.         requestCooldowns[player.Name]=nil
  103.     end
  104. end
  105.  
  106.  
  107.  
  108.  
  109.  
  110. --Fire a client to do an action
  111. function module:Fire(player,id,data)   
  112.     local remote = getEvent(player)
  113.     if(remote)then
  114.         remote:FireClient(player,id,data)
  115.     end
  116. end
  117.  
  118. --Fire all clients to do an action
  119. function module:FireAll(id,data)   
  120.     for _, player in pairs(game.Players:GetPlayers())do
  121.         local remote = getEvent(player)
  122.         if(remote)then
  123.             remote:FireClient(player,id,data)
  124.         end
  125.     end
  126. end
  127.  
  128.  
  129. --Handle an event
  130. function module:HandleEvent(id,cooldown,f)
  131.     actions[id] = {Cooldown = cooldown, PreviousEvents = {}, Run = f}
  132. end
  133.  
  134.  
  135.  
  136.  
  137.  
  138. --Request something from the client
  139. function module:Request(player, id, data)  
  140.     local remoteFunction = getFunction(player)
  141.     if(remoteFunction)then
  142.         return remoteFunction:InvokeClient(player, id, data)
  143.     end
  144. end
  145.  
  146.  
  147. --Handle a request from the client
  148. function module:HandleRequest(id, cooldown, f)
  149.     local newRequest = {Cooldown = cooldown}
  150.     function newRequest:Run(player,data)
  151.          return f(player,data)
  152.     end
  153.     request[id]=newRequest
  154. end
  155.  
  156.  
  157. return module
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement