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

Prevent people from firing too fast?

Where can I add a debounce to this gun script? This is the raycasting laser gun from the wiki.

local tool = script.Parent
local player = game:GetService("Players").LocalPlayer

tool.Equipped:connect(function(mouse)
	print("Gun equipped")
		
	mouse.Button1Down:connect(function()		
		print("Mouse pressed")
		
		local ray = Ray.new(tool.Barrel.CFrame.p, (mouse.Hit.p - tool.Barrel.CFrame.p).unit * 150)
		local part, position = workspace:FindPartOnRay(ray, player.Character, false, true)
	
		local beam = Instance.new("Part", workspace)
		beam.BrickColor = BrickColor.new("Bright yellow")
		beam.FormFactor = "Custom"
		beam.Material = "Neon"
		beam.Transparency = 0.25
		beam.Anchored = true
		beam.Locked = true
		beam.CanCollide = false
			
		local distance = (tool.Barrel.CFrame.p - position).magnitude
		beam.Size = Vector3.new(0.1, 0.1, distance)
		beam.CFrame = CFrame.new(tool.Barrel.CFrame.p, position) * CFrame.new(0, 0, - distance / 2)
		
		game:GetService("Debris"):AddItem(beam, 0.05)
		
		if part then
			local humanoid = part.Parent:FindFirstChild("Humanoid")
			
			if not humanoid then
				humanoid = part.Parent.Parent:FindFirstChild("Humanoid")
			end
			
			if humanoid then
				humanoid:TakeDamage(17)
			end
		end
	end)
end)

1 comment

0

Use a debounce/cooldown

1 answer

✓ Accepted answer
local tool = script.Parent
local player = game:GetService("Players").LocalPlayer
local reloadtime = 1 --Change this to be what you want to time the player has to wait to shoot after he shot, in seconds.

local debounce = false

tool.Equipped:connect(function(mouse)
    print("Gun equipped")
	    mouse.Button1Down:connect(function()    
		if debounce == false then
			debounce = true    
		        print("Mouse pressed")
		
		        local ray = Ray.new(tool.Barrel.CFrame.p, (mouse.Hit.p - tool.Barrel.CFrame.p).unit * 150)
		        local part, position = workspace:FindPartOnRay(ray, player.Character, false, true)
		
		        local beam = Instance.new("Part", workspace)
		        beam.BrickColor = BrickColor.new("Bright yellow")
		        beam.FormFactor = "Custom"
		        beam.Material = "Neon"
		        beam.Transparency = 0.25
		        beam.Anchored = true
		        beam.Locked = true
		        beam.CanCollide = false
		
		        local distance = (tool.Barrel.CFrame.p - position).magnitude
		        beam.Size = Vector3.new(0.1, 0.1, distance)
		        beam.CFrame = CFrame.new(tool.Barrel.CFrame.p, position) * CFrame.new(0, 0, - distance / 2)
		
		        game:GetService("Debris"):AddItem(beam, 0.05)
		
		        if part then
		            local humanoid = part.Parent:FindFirstChild("Humanoid")
		
		            if not humanoid then
		                humanoid = part.Parent.Parent:FindFirstChild("Humanoid")
		            end
		
		            if humanoid then
		                humanoid:TakeDamage(17)
		            end
		        end
			wait(reloadtime)
			debounce = false
		end
    end)
end)

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