Debounce method?

Hallo guys so I was wondering what’s the best method to debounce.
I’ve thought up of 2 ways which is

1. Value Object Debounce(Debris)
With this method I have to create an instance.
And Instances takes more memory than attributes.

2. Attribute Debounce(Coroutines to set nil)
Now this method might be more efficient than the first one because of the performance between the attribute and value object

the coroutine function I will be setting will be kind of debris.It will set the attribute value to nil thus deleting it.

But is the coroutine trade off worth it?

Note: I want the client to be able to check too.if you are gonna suggest a different method.

Why do you need a coroutine for a debounce?

I dont want my entire script yielding.

Using Debris is discouraged upon anyway, so yes, the attribute method would work. They should replicate server → client, just not the reverse.

1 Like

Hey I have another question wouldn’t using a time based debounce with attributes be more efficient because I just have to check the time that has elapsed and dont have to use coroutines anymore?

Stop using coroutines/wait for debounces! It’s so much cheaper and reliable to use os.clock()

local debounce = -math.huge -- This value could start at 0, but -math.huge is the most robust.
local function onSomething()
    local timestamp = os.clock()
    if timestamp > debounce then
        -- 2 second debounce
        debounce = timestamp + 2

        -- Do stuff here

    end
end

You shouldn’t need to store your debounce values in instances at all. It’s much simpler to do it all in Lua with ModuleScripts.

6 Likes

Yes I was going for that method however I’m using attributes to replicate it to clients.
Since module scripts dont sync with server and client.

As I have said here.

Hey I have another question wouldn’t using a time based debounce with attributes be more efficient because I just have to check the time that has elapsed and dont have to use coroutines anymore?

Ah didn’t see that when I started writing. Wait-based debounces are a pet peeve of mine and are found everywhere.

It really depends on your specific use case. Attributes/Instances will replicate to everyone, while you could use RemoteEvents if it’s meant for a specific player.

2 Likes