How to pass a function as a parameter?

I’m trying to make a function called “debounce.” the first parameter is how long you wait until you play the function given in the second parameter, which is where my problem is.
I get this error:

Players.OofDestroyer25.Backpack.Tool.client:26: attempt to call a nil value 

This is my script:

local function debounce(waittime, func)
	local db = false
	if db == false then
		db = true
		func()
		wait(waittime)
		db = false
	end
end

Heres where I call the function:

tool.Activated:Connect(function()
		debounce(0.4, Anim(anims.swing1, true, Enum.AnimationPriority.Action4))
end)

If anyone could tell me what’s wrong, I hope your pillow isn’t warm on both sides, and you have a amazing day.

I also forgot to mention, the code runs alright, but I’m worried the error will become a problem in the future.

Firstly, the “db” variable will always be false since it’s a local variable set to false every time you call the function. It would be best to set this as a global variable. Secondly, there’s a much more efficient way to do this.

local debouncedFunctions = {}

local function debounce(waitTime, functionName)
   if not table.find(debouncedFunctions, functionName) then

      functionName()
      table.insert(debouncedFunctions, functionName)

      task.delay(waitTime, function()
        table.remove(debouncedFunctions, table.find(debouncedFunctions, functionName))
      end)
     else
       return true
   end
end

Basically, this creates a table that will store all functions that are debounced and remove them after the wait time. If the function’s debounce isn’t over, it will return “true” for the debounce instead of adding it to the table again. If you have any questions, let me know. Please note that this is case-sensitive when calling the function name, or it will give you an error.

Edit: Also, be careful when calling a function like this, as it can easily error if it doesn’t recognize the function name as a parameter. I would recommend having a table of allowed functions which you check when the debounce function is called to ensure that the function really exists.

2 Likes

Holy Jesus thank you so much. This actually worked :heart_eyes:

The reason the error pops up is that whatever Anim() returns is being passed as an argument to the debounce function. For example, if Anim() returns nil, you would be passing nil as an argument to be called in the debounce function, which isn’t what you want. Hence, “attempt to call a nil value

In this case, you would want to use a tuple for your arguments.

local function debounce(waittime, func, ...)
	local db = false
	if db == false then
		db = true
		func(...) -- Use the tuple (...) as arguments for callback function
		wait(waittime)
		db = false
	end
end
tool.Activated:Connect(function()
	debounce(0.4, Anim, anims.swing1, true, Enum.AnimationPriority.Action4) -- Instead of calling Anim directly, pass the arguments of Anim through the debounce function
end)

And as @alreadyfans mentioned, your debounce wouldn’t work anyway so I suggest implementing the tuple method above with their code.

2 Likes

I’m assuming you’re doing this for proof of concept purposes but if not you can technically use task.delay()

Thank you for your response. I can finally continue my game :cowboy_hat_face:

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