Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local RunService = game:GetService("RunService");
- local ReplicatedStorage = game:GetService("ReplicatedStorage");
- local Players = game:GetService("Players");
- local Remotes = ReplicatedStorage.Remotes;
- local CooldownRemote = Remotes.CooldownRemote;
- local Modules = ReplicatedStorage.Modules;
- local Utility = Modules.Utility;
- local TablesLibrary = require(Utility.TablesLibrary);
- local FastSignal = require(Utility.FastSignal);
- local CooldownHandler = {};
- CooldownHandler.Characters = {};
- DEAD_CHARACTER_ERROR = "Character isn't alive";
- NOT_INITIALIZED_CHARACTER_ERROR = "Character isn't Initialized";
- export type Data = {
- Lifetime : number,
- StartTime : number | nil,
- };
- --// PUBLIC
- function CooldownHandler:GetData(Character)
- if not CooldownHandler.Characters[Character] then
- CooldownHandler.Characters[Character] = {};
- end
- return CooldownHandler.Characters[Character];
- end
- function CooldownHandler.Deinitialize(CharacterIndex : Model)
- assert(CharacterIndex, DEAD_CHARACTER_ERROR);
- assert(CooldownHandler.Characters[CharacterIndex], NOT_INITIALIZED_CHARACTER_ERROR);
- CooldownHandler.Characters[CharacterIndex] = nil;
- end
- function CooldownHandler:CheckCooldown(CharacterIndex : Model, cooldownIndex : string) : nil
- local Profile = CooldownHandler:GetData(CharacterIndex);
- local Data = Profile[cooldownIndex];
- local isCooldown = if Data then TablesLibrary.Length(Data) >= 1 else false;
- return isCooldown;
- end
- if RunService:IsClient() then
- function CooldownHandler:UpdateCooldown(CharacterIndex : Model, cooldownIndex : string, Data : Data) : nil
- local Profile = CooldownHandler:GetData(CharacterIndex);
- local Lifetime = Data.Lifetime;
- local StartTime = Data.StartTime or os.clock();
- if not Profile[cooldownIndex] then
- Profile[cooldownIndex] = {};
- end
- Profile[cooldownIndex][StartTime] = {
- StartTime = StartTime,
- Lifetime = Lifetime,
- };
- coroutine.wrap(function()
- task.wait(Lifetime);
- Profile[cooldownIndex][StartTime] = nil;
- end)()
- end
- return CooldownHandler;
- elseif RunService:IsServer() then
- function CooldownHandler:UpdateCooldown(CharacterIndex : Model, cooldownIndex : string, Data : Data) : nil
- local Profile = CooldownHandler:GetData(CharacterIndex);
- local Lifetime = Data.Lifetime;
- local StartTime = Data.StartTime or os.clock();
- if not Profile[cooldownIndex] then
- Profile[cooldownIndex] = {};
- end
- Profile[cooldownIndex][StartTime] = {
- StartTime = StartTime,
- Lifetime = Lifetime,
- };
- coroutine.wrap(function()
- task.wait(Lifetime);
- Profile[cooldownIndex][StartTime] = nil;
- end)()
- local Player = Players:GetPlayerFromCharacter(CharacterIndex);
- if Player then
- CooldownRemote:FireClient(Player, cooldownIndex, {
- StartTime = StartTime,
- Lifetime = Lifetime,
- });
- end
- end
- return CooldownHandler;
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement