Help fix a script using tables

local CooldownModule = {}

local cooldownPlayer = {}

--How i check a cooldown in a script :
--cooldownPlayer[player] - os.time() < cooldownTime (Let's say 5 seconds)
--os.time() is the time that has elpased since 1970

--Update the time 
function CooldownModule.UpdateCooldown(player: Player)
	cooldownPlayer[player] = os.time()
end

--Remove cooldown
function CooldownModule.ResetCooldown(player: Player)
	cooldownPlayer[player] = 0
end

--Returns the cooldown of a player
function CooldownModule.GetCoodlwon(player: Player)
	return cooldownPlayer[player]
end

return CooldownModule

this is a cooldowns module i saw on the devforum. i wanted to use it but modify it so that it can also store cooldowns for separate abilities, for the same player, in the same table.
only problem is, i tried doing this and it didnt work (because this is how i thought it would work, i dont know why)

cooldownPlayer[player][abilityName] = 0

can someone help me and tell me how to actually do this? i thought of using something like nested tables but have no idea how they work.
if you know a better way to do this please tell me.

here’s how it should be done:

local CooldownModule = {}
local cooldownPlayer = {}

-- This should be called when the player enters, or anywhere u find it suitable to be called:
-- Just make sure this is called before using the module's other functions,
function CooldownModule.Setup_player(player: Player)
	cooldownPlayer[player] = {}
    cooldownPlayer[player] = {
       attack = os.time(),
       other_attack = os.time()
    }
end

--Update the time 
function CooldownModule.UpdateCooldown(player: Player, ability)
	cooldownPlayer[player][ability] = os.time()
end

--Remove cooldown
function CooldownModule.ResetCooldown(player: Player, ability)
	cooldownPlayer[player][ability] = 0
end

--Returns the cooldown of a player
function CooldownModule.GetCoodlwon(player: Player)
	return cooldownPlayer[player]
end

return CooldownModule

how would i loop thorugh all attacks in order to set all those os.time to 0 all at once?

and when calling the function like update cooldown, do i pass the attack name like in the example attack / other_attack as a string?

yes you do need to pass in the ability names like “attack”… to reset a certain cooldown
here i made a function that resets all cooldowns

local CooldownModule = {}
local cooldownPlayer = {}

-- This should be called when the player enters, or anywhere u find it suitable to be called:
-- Just make sure this is called before using the module's other functions,
function CooldownModule.Setup_player(player: Player)
	cooldownPlayer[player] = {}
    cooldownPlayer[player] = {
       attack = os.time(),
       other_attack = os.time()
    }
end

--Update the time 
function CooldownModule.UpdateCooldown(player: Player, ability)
	cooldownPlayer[player][ability] = os.time()
end

-- reset an ability's cooldown
function CooldownModule.ResetCooldown(player: Player, ability)
	cooldownPlayer[player][ability] = 0
end

-- resets all abilities's cooldown
function CooldownModule.ResetAllCooldowns(player: Player)
	for ability, value in cooldownPlayer[player] do
        value = 0
    end
end

--Returns the cooldown of a player
function CooldownModule.GetCoodlwon(player: Player)
	return cooldownPlayer[player]
end

return CooldownModule

i still have a question

function CooldownModule.CheckCooldown(player: Player, ability, cooldownTime:number)
	if cooldownPlayer[player][ability] - os.time() < cooldownTime then
		return true
	else
		return false
	end
end

i added this function to the module
and did this for the ability

local CD_Check = CooldownModule.CheckCooldown(plr, "Uppercut", 3)
	if not CD_Check then warn("not passed") return end
	CooldownModule.UpdateCooldown(plr, "Uppercut")
	print("passed")

and it seems it has no cooldown

this is how the module looks

local CooldownModule = {}

local cooldownPlayer = {}

--How i check a cooldown in a script :
--cooldownPlayer[player] - os.time() < cooldownTime (Let's say 5 seconds)
--os.time() is the time that has elpased since 1970

function CooldownModule.SetupCooldowns(player: Player)
	cooldownPlayer[player] = {}
	cooldownPlayer[player] = {
		M1s = os.time(),
		Uppercut = os.time(),
	}
	print("setup complete")
end




--Update the time 
function CooldownModule.UpdateCooldown(player: Player, ability)
	cooldownPlayer[player][ability] = os.time()
end


--Remove cooldown
function CooldownModule.ResetCooldown(player: Player, ability)
	cooldownPlayer[player][ability] = 0
end


function CooldownModule.ResetAllCooldowns(player: Player)
	for ability, value in cooldownPlayer[player] do
		value = 0
	end
end


--Returns the cooldown of a player
function CooldownModule.GetCooldown(player: Player, ability)
	return cooldownPlayer[player][ability]
end


function CooldownModule.CheckCooldown(player: Player, ability, cooldownTime:number)
	if cooldownPlayer[player][ability] - os.time() < cooldownTime then
		return true
	else
		return false
	end
end

return CooldownModule

forgot to say, i already ran the setup in a different server script

ok so i rewrote some stuff of ur module, it seems to be working fine for me:

local CooldownModule = {}

local cooldownPlayer = {}

--How i check a cooldown in a script :
--cooldownPlayer[player] - os.time() < cooldownTime (Let's say 5 seconds)
--os.time() is the time that has elpased since 1970

function CooldownModule.SetupCooldowns(player: Player)
	cooldownPlayer[player] = {}
	cooldownPlayer[player] = {
		M1s = os.time(),
		Uppercut = os.time(),
	}
end

function CooldownModule.CheckCooldown(player: Player, ability, cooldownTime:number)
	return os.time() - cooldownPlayer[player][ability] < cooldownTime
end

--Update the time 
function CooldownModule.UpdateCooldown(player: Player, ability)
	cooldownPlayer[player][ability] = os.time()
end


--Remove cooldown
function CooldownModule.ResetCooldown(player: Player, ability)
	cooldownPlayer[player][ability] = 0
end


function CooldownModule.ResetAllCooldowns(player: Player)
	for ability, value in cooldownPlayer[player] do
		value = 0
	end
end


--Returns the cooldown of a player
function CooldownModule.GetCooldown(player: Player, ability)
	return cooldownPlayer[player][ability]
end

return CooldownModule

script:

local module = require(script.ModuleScript)

game.Players.PlayerAdded:Connect(function(player: Player) 
	module.SetupCooldowns(player)
	
	task.delay(3.5, function()
		local on_cooldown = module.CheckCooldown(player, "Uppercut", 3) -- if true then on cooldown
		if not on_cooldown then
			print("this ability isnt on cooldown")
		end
	end)
end)

Seems to be working now, thanks a lot

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.