Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How add a cooldown for an ability script that works in conjunction with a module script?

Basically what I wrote in the question is what I need help on. I tried to add a cooldown in the module script then get it from the local script but I get this error

21:01:18.542 Players.Funnyskyswagway3.PlayerScripts.Keybinds:10: attempt to index nil with ‘Cooldown’ - Client - Keybinds:10

Local Script:

local GameFolder = game.ReplicatedStorage.GameFolder
local Modules = GameFolder.Modules
local PearlModule = require(Modules.PearlAbilities)
local Debounce = true

local UserInputService = game:GetService("UserInputService")
UserInputService.InputBegan:Connect(function(hit, chat)
	if chat then return end
	local ActivateAbility = PearlModule[hit.KeyCode] 
	local Cooldown = ActivateAbility.Cooldown
	if ActivateAbility and Debounce == true then
		Debounce = false
		ActivateAbility.Activate()
		wait(Cooldown)
		Debounce = true
	end
end)

Module Script:

local TweenService = game:GetService("TweenService")
local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:wait()
local Abilitys = {
	[Enum.KeyCode.Q] = {
		Activate = function()
			local Gem = Player.Character:WaitForChild("Gem")


			if Gem.Light.Brightness == 0 then
				Gem.Material = Enum.Material.Neon
				TweenService:Create(Gem.Light,TweenInfo.new(1), {Brightness = 10} ):Play() 

			else
				Gem.Material = Enum.Material.SmoothPlastic
				TweenService:Create(Gem.Light,TweenInfo.new(0.5), {Brightness = 0} ):Play()
			end
			
		end,
	}
	
}
return Abilitys

1 answer

You reference cooldown with the table in the modulescript, but It doesnt have a cooldown.

The part that Im talking about is

Abilitys[Enum.KeyCode.Q]

Cooldown tries to find a cooldown inside of the table, even though it doesnt exist

Im just going to copy your code and add a cooldown so you can understand

local Abilitys = {}

local keyQ = Abilitys[Enum.KeyCode.Q]

keyQ.Activate = function()
	local Gem = Player.Character:WaitForChild("Gem")

	 if Gem.Light.Brightness == 0 then
		Gem.Material = Enum.Material.Neon
		TweenService:Create(Gem.Light,TweenInfo.new(1), {Brightness = 10} ):Play()
	else
		 Gem.Material = Enum.Material.SmoothPlastic
		TweenService:Create(Gem.Light,TweenInfo.new(0.5), {Brightness = 0} ):Play()
	end
end

keyQ.Cooldown = --whatever you want your cooldown to be

I recommend you write it in the way I do to make it easier to read

using .to create a function or [] is the same as what you did

You can just copy this into your modulescript, and it should work

Hope I helped!

This discussion is preserved as it was. New answers, comments, and votes are closed.