How do I add a cooldown to my gun?

So I’m making a fighting game with simple guns, no ray cast or anything, but I need to add a debounce to my gun system, the problem is I don’t know where to put it. I got the system from a tutorial and I’m new to scripting. Here is the script.

Start of code

script.Parent.RemoteEvent.OnServerEvent:Connect(function(player, target)
target.Humanoid:TakeDamage(30)
end)

End of code

I don’t know how to put it in the script thingy but yeah

I just need a simple debounce in the code, thanks! :smiley:

1 Like

You can do something like this:

local debounce = false
local fireRate = 2 -- (time between each shot)

script.Parent.RemoteEvent.OnServerEvent:Connect(function(player, target)
if debounce == false then
target.Humanoid:TakeDamage(30)
task.wait(fireRate)
debounce = false
end
end)
1 Like

Thank you, but I’m away and don’t have my computer so I’ll get back to you and see if it works. Thanks again!

1 Like

Also how did you put the script inside the thing?

You use ``` at the start and end of your script.

Also small little mistake, you need debounce = true in it too
so heres the updated:

local debounce = false
local fireRate = 2 -- (time between each shot)

script.Parent.RemoteEvent.OnServerEvent:Connect(function(player, target)
if debounce == false then
debounce = true
target.Humanoid:TakeDamage(30)
task.wait(fireRate)
debounce = false
end
end)
1 Like

Ok thank you, I’ll try this one when I get back. Also here’s a test run:

local debounce == banana local GunSystem==idk what I’m doing

test to see if codeblock works and it does

THANK YOU SO MUCH IT WORKED! :)))))

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