Can I handle abilities any better?

LocalScript:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local UserInputService = game:GetService("UserInputService")
local Players = game:GetService("Players")

local player = Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local Skillset = require(ReplicatedStorage.Modules.Skillsets:FindFirstChild(character.Humanoid:GetAttribute("Class")))

UserInputService.InputBegan:Connect(function(input, gameProcessed)
	if gameProcessed then return end
	Skillset:Cast(input.KeyCode.Name)
end)

Module:

local module = {}
local cooldowns = {}
local skillset = {
	["One"] = {
		info = {
			damage = 6,
			cooldown = 1
		},
		func = function()
			print("1")
		end
	},
	["Two"] = {
		info = {
			damage = 12,
			cooldown = 2
		},
		func = function()
			print("2")
		end
	},
	["Three"] = {
		info = {
			damage = 12,
			cooldown = 4
		},
		func = function()
			print("3")
		end
	},
	["Four"] = {
		info = {
			damage = 24,
			cooldown = 8
		},
		func = function()
			print("4")
		end
	}
}

function cooldown(skill:string, length:number)
	cooldowns[skill] = true
	task.wait(length)
	cooldowns[skill] = false
end

function module:Cast(skill:string)
	local s = skillset[skill]
	if s then
		if cooldowns[s] then return end
		s.func()
		cooldown(s, s.info.cooldown)
	end
end

return module
3 Likes