Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How can I stop the player from spamming my burst fire gun?

Hi, i'm making a burst fire gun which does infact work. It needs to not get spammed, i've tried making a cooldown but it didn't work. Basically, I want to add a debounce before you can start shooting again.

Here is the scripts.

Tool

local plr = game.Players.LocalPlayer
local mouse = plr:GetMouse()
local pistol = script.Parent
local tool = script.Parent

script.Parent.Activated:Connect(function()
	for i = 1,3 do
		print(mouse.Target)
		local Target = mouse.Target
		workspace.WeaponFired:InvokeServer(plr, Target, pistol)
		wait(0.8)
	end
end)

ServerSide

local FiredWep = workspace.WeaponFired

FiredWep.OnServerInvoke = function(plr,A1,A2,A3)
	print(A1,A2,A3)
	A2.Parent.Humanoid:TakeDamage(35)
	for i = 1,3 do
		if A2.Name == "Head" then
			A2.Parent.Humanoid:TakeDamage(125)
			wait(.65)
		end
	end
end

[SerpentineKing]: Added Proper Indentation

1 comment

0

Go to the wicky article about denounce it’ll explain in well

2 answers

Well you can use debounce, this concept helps you make a some sort of "cooldown". It's like when you used your gun the first time you would need to wait something like a second or whatever amount you want, than you'll be able to use it again. We can set a variable that defines whether the gun was used or not, if it's true then that means that you can't use the gun yet until the cooldown is over and when that cooldown is over this variable is turned to false again which means we can use it, and of course each time we use the gun again we change that variable to true and make a cooldown.

local debounce = false --this is the variable that defines whether it has been used or no, and debounce is just a common name you can name it whatever you want

part.Touched:Connect(function()
   --the touched event is a good example because it keeps on firing as long as you're still touching so we can make a cooldown so it only fires for example every 5 minutes
   if debounce == false then --we check if was used before
      debounce = true --we indicate that it has been used
      
      --do whatever you want here
      
      wait(5)  --this is our cooldown 
      debounce = false --and you can see we turn it back to false after the cooldown is over
   end

end)

I hope you get what we're doing, you can apply this to your thing now

1 comment

0

^ Yeah, I tried that but it didnt work, was a bool value.

the fact that you mention, "debouncing doesn't work" indicates that something is wrong with either the client to server invocation, or the server handling of client request. because debounce should ALWAYs work..

so here is a blue print that you can use to ensures code maintainability, clean coding as the code gets bigger, and gives you full control of what's happening.

Client side:

local remote_function = game.etc.etc --directory of remote function
local tool = script.Parent --the tool object
local handle = tool.Handle
local debounce = false
local db_length = 0.2 --length of debounce

tool.Equipped:Connect(function(mouse)
	mouse.Button1Down:Connect(function()
		if(not debounce) then
			debounce = true
			--invoke server with mouse position
			remote_event:InvokeServer(mouse.Hit.p,handle);
			wait(db_length)
			debounce = false
		end
	end)
end)

Server side:

local remote_function = game.etc.etc --directory of remote function
remote_function.OnServerInvoke = function(player,mousePosition,handle)
	--code to shoot bullet/ray or whatever, from handle.Position or Cframe, to mouse position.
end

This discussion is preserved as it was. New answers, comments, and votes are closed.