Advertisement
emilistt

CooldownHandler.lua

Apr 3rd, 2022
807
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.90 KB | None | 0 0
  1. local RunService = game:GetService("RunService");
  2. local ReplicatedStorage = game:GetService("ReplicatedStorage");
  3. local Players = game:GetService("Players");
  4.  
  5. local Remotes = ReplicatedStorage.Remotes;
  6.  
  7. local CooldownRemote = Remotes.CooldownRemote;
  8.  
  9. local Modules = ReplicatedStorage.Modules;
  10. local Utility = Modules.Utility;
  11.  
  12. local TablesLibrary = require(Utility.TablesLibrary);
  13. local FastSignal = require(Utility.FastSignal);
  14.  
  15. local CooldownHandler = {};
  16. CooldownHandler.Characters = {};
  17.  
  18. DEAD_CHARACTER_ERROR = "Character isn't alive";
  19. NOT_INITIALIZED_CHARACTER_ERROR = "Character isn't Initialized";
  20.    
  21. export type Data = {
  22.     Lifetime : number,
  23.     StartTime : number | nil,
  24. };
  25.  
  26. --// PUBLIC
  27.  
  28. function CooldownHandler:GetData(Character)
  29.     if not CooldownHandler.Characters[Character] then
  30.         CooldownHandler.Characters[Character] = {};
  31.     end
  32.  
  33.     return CooldownHandler.Characters[Character];
  34. end
  35.  
  36. function CooldownHandler.Deinitialize(CharacterIndex : Model)
  37.     assert(CharacterIndex, DEAD_CHARACTER_ERROR);
  38.     assert(CooldownHandler.Characters[CharacterIndex], NOT_INITIALIZED_CHARACTER_ERROR);
  39.  
  40.     CooldownHandler.Characters[CharacterIndex] = nil;
  41. end
  42.  
  43. function CooldownHandler:CheckCooldown(CharacterIndex : Model, cooldownIndex : string) : nil
  44.     local Profile = CooldownHandler:GetData(CharacterIndex);
  45.     local Data = Profile[cooldownIndex];
  46.  
  47.     local isCooldown = if Data then TablesLibrary.Length(Data) >= 1 else false;
  48.  
  49.     return isCooldown;
  50. end
  51.  
  52. if RunService:IsClient() then
  53.     function CooldownHandler:UpdateCooldown(CharacterIndex : Model, cooldownIndex : string, Data : Data) : nil
  54.         local Profile = CooldownHandler:GetData(CharacterIndex);
  55.        
  56.        
  57.         local Lifetime = Data.Lifetime;
  58.         local StartTime = Data.StartTime or os.clock();
  59.        
  60.         if not Profile[cooldownIndex] then
  61.             Profile[cooldownIndex] = {};
  62.         end
  63.        
  64.         Profile[cooldownIndex][StartTime] = {
  65.             StartTime = StartTime,
  66.             Lifetime = Lifetime,
  67.         };
  68.        
  69.         coroutine.wrap(function()
  70.             task.wait(Lifetime);
  71.            
  72.             Profile[cooldownIndex][StartTime] = nil;
  73.         end)()
  74.     end
  75.    
  76.     return CooldownHandler;
  77. elseif RunService:IsServer() then
  78.     function CooldownHandler:UpdateCooldown(CharacterIndex : Model, cooldownIndex : string, Data : Data) : nil
  79.         local Profile = CooldownHandler:GetData(CharacterIndex);
  80.  
  81.         local Lifetime = Data.Lifetime;
  82.         local StartTime = Data.StartTime or os.clock();
  83.        
  84.         if not Profile[cooldownIndex] then
  85.             Profile[cooldownIndex] = {};
  86.         end
  87.  
  88.         Profile[cooldownIndex][StartTime] = {
  89.             StartTime = StartTime,
  90.             Lifetime = Lifetime,
  91.         };
  92.        
  93.         coroutine.wrap(function()
  94.             task.wait(Lifetime);
  95.  
  96.             Profile[cooldownIndex][StartTime] = nil;
  97.         end)()
  98.            
  99.         local Player = Players:GetPlayerFromCharacter(CharacterIndex);
  100.  
  101.         if Player then
  102.             CooldownRemote:FireClient(Player, cooldownIndex, {
  103.                 StartTime = StartTime,
  104.                 Lifetime = Lifetime,
  105.             });
  106.         end
  107.     end
  108.  
  109.     return CooldownHandler;
  110. end
  111.  
  112.  
  113.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement