Advertisement
TaylorsRus

SkillHandler Mockup (Server)

May 9th, 2023
978
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.91 KB | None | 0 0
  1. local SkillHandler = {}
  2. SkillHandler.Modules = {}
  3.  
  4. local ReplicatedStorage = game:GetService("ReplicatedStorage")
  5.  
  6. local CooldownDurations = require(script:FindFirstChild("CooldownDurations"))
  7.  
  8. local SkillsFolder = ReplicatedStorage:FindFirstChild("SkillTypes")
  9.  
  10. local DEFAULT_COOLDOWN = 5
  11.  
  12. local function ReturnVariations(Skill)
  13.     local Functions = {}
  14.  
  15.     for Name, Callback in require(Skill) do
  16.         if type(Callback) ~= "function" then continue end
  17.  
  18.         Functions[Name] = Callback
  19.     end
  20.  
  21.     return Functions
  22. end
  23.  
  24. function SkillHandler:Main(Player)
  25.     for _,Style in SkillsFolder:GetDescendants() do
  26.         if not Style:IsA("ModuleScript") then continue end
  27.        
  28.         self.Modules[Style] = {
  29.             Type = Style:FindFirstAncestorOfClass("Folder"),
  30.             Variations = ReturnVariations(Style)
  31.         }
  32.     end
  33. end
  34.  
  35. function SkillHandler:StartCooldown(Player, Skill)
  36.     local Duration = CooldownDurations[Skill] or DEFAULT_COOLDOWN
  37.     local CooldownBegan, Cooldowns = os.clock(), self.PlayerData:GetValue(Player, "Cooldowns")
  38.    
  39.     table.insert(Cooldowns, Skill)
  40.    
  41.     task.spawn(function()
  42.         repeat task.wait() until os.clock() - CooldownBegan > Duration
  43.  
  44.         table.remove(Cooldowns, self.Shared["TableUtil"]:GetIndex(Cooldowns, Skill))
  45.     end)
  46. end
  47.  
  48. function SkillHandler:CastSkill(Player, Skill)
  49.     local Values = self.PlayerData:GetAllValues(Player)
  50.     if Values.Attacking or Values.Guarding then return end
  51.    
  52.     local OnCooldown = table.find(self.PlayerData:GetValue(Player, "Cooldowns"), Skill)
  53.     if OnCooldown then
  54.         warn(Skill,"is currently on cooldown.")
  55.         return
  56.     end
  57.    
  58.     local PerformedStyle = nil
  59.     for Style, StyleData in self.Modules do
  60.         if not self.Shared:HasFunction(Skill, require(Style)) then continue end
  61.        
  62.         PerformedStyle = Style
  63.     end
  64.    
  65.     self:StartCooldown(Player, Skill)
  66.     self.Shared["ClientServer"]:FireAllClients("EffectsHandler", "CastSkill", {
  67.         Style = PerformedStyle,
  68.         Variation = Skill,
  69.         Caster = Player
  70.     })
  71. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement