How do I add an inventory cooldown script?

Basically whenever a player wants to spam switch items with the inventory system I want to make it where there is a cooldown for that so lets say a player has a Scar and an AK or whatever I want a cooldown where if a player switches from a scar to AK he has to wait like a second or however long I can set it too before he can switch back to a scar or another gun

3 Likes

You should research roblox debounces

1 Like

Add a debounce.
Char limit 30 :frowning:

1 Like

How do I add debounce with the inventory?

1 Like

A lot of people are saying “debounce”. While this is true, they’re not specifying how you would go about this. You’re not a beginner scripter, and I’m sure you know what a debounce is.

So, here’s my suggestion, this is a tick() debounce:

local debounce = tick()
local debounceTime = 1

if (tick() - debounce) > debounceTime then
	-- Your code, switch weapon probably
	debounce = tick()
end

This is a suggestion, tick() might be deprecated (I’m unsure).
I hope this helps you with your code!

Also, a bit of info on tick().
Tick basically gets the time passed since 1st January, 1970.

local debounce = tick() -- Stores that time in debounce

So, when you do:

(tick() - debounce) > debounceTime 

You’re basically doing:
(Time passed - Stored time) > 1 second

3 Likes

I am a beginner scripter and I have no idea how to code lmao im only a builder sadly

1 Like

That’s perfectly fine.

A debounce is essentially just a cooldown.
Think of it like this:

local debounce = true
if debounce then
	debounce = false
	-- code stuff (maybe a task.wait(1) as a delay)
	debounce = true
end

The if statement will only run if debounce = true, so right after the if statement, we set the debounce to false to prevent spamming until the code has ended, hence we set debounce back to true at the end.

I hope my explanation wasn’t too complicated.

1 Like
local debounce = false
local WaitTime = 1

while true do
if debounce == false then
debounce = true
print("test") 
wait(WaitTime) 
debounce = false
end
end
2 Likes

What script type should it be and where should I put it?

Where you placed your gun code

2 Likes

Oh alright the main code is obstruficated btw so would I need to do anything else?

1 Like

No.

-------Roblox char limit------------

1 Like