Advertisement
Guest User

Cooldown Handling Module

a guest
Aug 10th, 2021
265
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.03 KB | None | 0 0
  1. --//Hopefully you find this module in some way usefull
  2. --//More info here https://devforum.roblox.com/t/1399553
  3. --//Created by AllAllLol1\\
  4.  
  5. local Cooldown = {}
  6. local CooldownList = {}
  7.  
  8. local function UpdateTime(startTime, tableName, valueName) --This function is completely additional and can be removed or edited if you know what your doing
  9.     if CooldownList[tableName] then
  10.         if CooldownList[tableName][valueName] then
  11.             local OriginalTime = CooldownList[tableName][valueName] --Original cooldown duration
  12.            
  13.             while wait() do
  14.                 local currentTime = os.time()
  15.                 local timePassed = currentTime - startTime
  16.        
  17.                 local timeLeft = OriginalTime - timePassed
  18.                 if OriginalTime >= timePassed then --I dont want the local [timeLeft] variable to change the actual variable inside the table to a negative integer
  19.                     CooldownList[tableName][valueName] = timeLeft
  20.                 else
  21.                     CooldownList[tableName][valueName] = 0 --Else it will just be 0
  22.                     break --Break the while loop
  23.                 end
  24.             end
  25.         end
  26.     end
  27. end
  28.  
  29. local function WaitCooldown(tableName, valueName, Time) --This function will wait for the cooldown to finish and then delete the value from the player table or the player table itself if its empty
  30.     local startTime = os.time()
  31.     local newThread = coroutine.create(UpdateTime)
  32.     coroutine.resume(newThread, startTime, tableName, valueName)
  33.    
  34.     wait(Time) --This line waits in seconds (local [UpdateTime] function does not change local [Time] variable)
  35.  
  36.     for i, ii in next, CooldownList[tableName] do
  37.         if i == valueName then
  38.             CooldownList[tableName][ii] = nil
  39.             table.remove(CooldownList[tableName],i)
  40.         end
  41.     end
  42.  
  43.     if #CooldownList[tableName] == 0 then
  44.         CooldownList[tableName] = nil
  45.     end
  46. end
  47.  
  48. function Cooldown.Add(tableName, valueName, cooldown) -- This function will insert a table for each diffirent player with diffirent cooldown values for diffirent valueName
  49.     if tonumber(cooldown) ~= nil then --This line is to make sure that passed variable [cooldown] isn't nil and has interger(s)
  50.         if CooldownList[tableName] then
  51.             table.insert(CooldownList[tableName], valueName)
  52.             CooldownList[tableName][valueName] = cooldown
  53.             local newThread = coroutine.create(WaitCooldown)
  54.             coroutine.resume(newThread, tableName, valueName, cooldown)
  55.             return true
  56.         else
  57.             CooldownList[tableName] = {[valueName] = cooldown}
  58.             local newThread = coroutine.create(WaitCooldown)
  59.             coroutine.resume(newThread, tableName, valueName, cooldown)
  60.             return true
  61.         end
  62.     end
  63.     return false
  64. end
  65.  
  66. function Cooldown.Check(tableName, valueName) --This function will check [CooldownList] table in order to know if the value already exists
  67.     if CooldownList[tableName] then
  68.         if CooldownList[tableName][valueName] then
  69.             local timeLeft = CooldownList[tableName][valueName] --If the local [UpdateTime] function is used it will return the time that is left else it will return the starting local [cooldown] variable in Module [Add] function
  70.             return false, timeLeft
  71.         else
  72.             return true
  73.         end
  74.     else
  75.         return true
  76.     end
  77. end
  78.  
  79. return Cooldown
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement