Gun shooting even though it shouldn't be

So, I’m working on my cruiser main guns and I have a variable known as “canShoot” and its by default enabled and disabled after the gun is shot, then re-enables right when the cooldown finishes.

local fire = game.ReplicatedStorage.FireGunEvent
game.ReplicatedStorage.FireGunEvent.OnServerEvent:Connect(function()
	print("Checking if fire is valid...")
	local canShoot = true
	if canShoot == true then
		canShoot = false
		print("Valid")
		local muzzle1 = script.Parent.Parent.Parent.Muzzle1
		local muzzle2 = script.Parent.Parent.Parent.Muzzle2
		local muzzle3 = script.Parent.Parent.Parent.Muzzle3
		script.Parent.Parent.Fire:Play()
		
		muzzle1.Emitter.Enabled = true
		muzzle2.Emitter.Enabled = true
		muzzle3.Emitter.Enabled = true
		
		wait(0.5)
		
		muzzle1.Emitter.Enabled = false
		muzzle2.Emitter.Enabled = false
		muzzle3.Emitter.Enabled = false
		
		wait(7)
		script.Parent.Parent.Reload:Play()
		wait(3)
		canShoot = true
	end
end)

But it still lets you shoot even before the cooldown finishes.
If someone can figure this out I’d appreciate it.

Thanks,

– Luke

Your debounce is serverside and a new debounce is created every time the event* is fired.

You can add the debounce clientside. or developed a better Serverside debounce

created a variable on the client side but it doesnt seem to work either

local usp = game:GetService("UserInputService")
local canShoot = true
local function test(input, gameProcessedEvent)
	if input.UserInputType == Enum.UserInputType.Keyboard then
		if input.KeyCode == Enum.KeyCode.F then
			if canShoot == true then
				print("Fire")
				local fire = game.ReplicatedStorage:WaitForChild("FireGunEvent")
				fire:FireServer()
			end
		end
	end
end

usp.InputBegan:Connect(test)
if canShoot == true then
canShoot =false -- you forgot
print("Fire")
local fire = game.ReplicatedStorage:WaitForChild("FireGunEvent")
fire:FireServer()
wait(1) -- the cooldown
canShoot = true -- last part
end
1 Like