Help figuring out logic for tool cooldowns

I’m trying to create a system which can detect tools with certain attributes and act differently based on them.

To put simply, I want a system which applies a slight cooldown when you’re first taking out your main weapon; it should NOT apply a cooldown if you’re holding an ability tool and switch to a weapon or the other way around. I’m struggling to figure out the exact logic for this as I keep running into issues such as if i hold an ability then put it away, if i grab my main weapon since my last tool was an ability the cooldown won’t apply. It’s quite similar but the difference is the time between the actions. I’ve tried recording this time but that raises other issues such as what if I simply hold the ability tool out? It would conflict.

I’m very stumped. Some code examples or ideas on how I could achieve this would be appreciated.

It sounds like it would be viable for you to list all of the state transitions. Have you tried building a state machine?

e.g. Suppose you have three states: A, B, and C.

Going from A to B should take 1 second, but going to A to C should take 2 seconds. All other transitions (e.g. C to A) should take 3 seconds.

More visually:
image

And possibly in practice:

local current_state = "A"
local function TransitionState(new_state)
    if current_state == "A" and new_state == "B" then
        return 1
    end
    if current_state == "A" and new_state == "B" then
        return 2
    end
    return 3
end
local timeout = TransitionState("A")
task.wait(timeout)
timeout = TransitionState("C")
task.wait(timeout)
-- ... 
1 Like

Okay I see where you’re going with this. I’l do some brainstorming on the idea and read up more. I think it could possibly work :smile: