I need help with my server-sided debounce module script

I want to make a function to set the debounce value for the move.
I have not found any solutions in the developer forum if you found any please send them.

Here is the debounce module

local Debounce = {}

local Debounces = {}

function Debounce:AddDebounce(Player, Move)
	if not Debounces[Player.Name] then
		Debounces[Player.Name] = Debounces[Player.Name] or {}
	end
	if not Debounces[Player.Name][Move] then
		Debounces[Player.Name][Move] = false
	else
		return warn(Move.." Debounce already exists.")
	end
	print(Debounces)
end

function Debounce:Set(Player, Move, SetValue)
	if Debounces[Player.Name] and Debounces[Player.Name][Move] then
		Debounces[Player][Move] = SetValue
	else
		return warn("Could not find debounce")
	end
end

return Debounce

Here is the moveset module

local Moveset = {
	["Combat"] = {
		["M1"] = {
			Name = "M1";
			Cooldown = .25;
			FinishCooldown = .7;
			Key = Enum.UserInputType.MouseButton1
		},
		["Block"] = {
			Name = "Block";
			Cooldown = .25;
			Key = Enum.KeyCode.F
		},
		["Dash"] = {
			Name = "Dash";
			Cooldown = 1;
			Key = Enum.KeyCode.Q
		}
	}
}

return Moveset

And here is the main script

local Moveset = require(script.Parent.Modules.Moveset)
local Debounce = require(script.Parent.Modules.Debounce)

game.Players.PlayerAdded:Connect(function(Player)
	Debounce:AddDebounce(Player, Moveset.Combat.M1.Name)
	Debounce:Set(Player, Moveset.Combat.M1.Name, true)
end)

The part I am having issues with

function Debounce:Set(Player, Move, SetValue)
	if Debounces[Player.Name] and Debounces[Player.Name][Move] then
		Debounces[Player][Move] = SetValue
	else
		return warn("Could not find debounce")
	end
end

I need help since it can’t find the debounce it is looking for.

you are setting the value to false, therefore

it will show up here as false and the if statement will not run

Oh how could I fix that problem?

I am trying to check if the move is actually there.

try replacing this with

if Debounces[Player.Name] and Debounces[Player.Name][Move] ~= nil then
		Debounces[Player][Move] = SetValue
	else

this way the if statement should fail if the debounce does not exist in the table, but it should pass if the debounce is equal to false

Is there a way I could make

Debounces[Player.Name][Move] ~= nil

Shorter?

no, because you are trying to specifically check if the value is nil or not, and thats the simplest way

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