The delay does not work in the local script

I’m don’t know why the delay won’t work. No one has such problems…

local player = game:GetService("Players").LocalPlayer
local mouse = player:GetMouse()
local UserInput = game:GetService("UserInputService")
local ContexAction = game:GetService("ContextActionService")
script.Parent.Handle.Fire.Volume = 0 -- Server sound
script.Parent.Handle.GunMuzzlePoint.Flash.Lifetime = NumberRange.new(0) -- -- Server particle
equiped = false

script.Parent.Equipped:Connect(function()
	ContexAction:BindAction("Reload",function(name,state,obj)
		if state == Enum.UserInputState.Begin then
			script.Parent.ReloadEvent:FireServer()
		end
	end,true,Enum.KeyCode.R)
	script.Parent.Handle.EquippedSound:Play()
	equiped = true
end)
script.Parent.Unequipped:Connect(function()
	ContexAction:UnbindAction("Reload")
	script.Parent.Handle.EquippedSound:Play()
	equiped = false
end)
firing = false
mouse.Button1Down:Connect(function()
	if equiped and not firing and not script.Parent.NoAmmo.Value == true then
		firing = true
		while firing == true and equiped do
			local firesound = script.Parent.Handle.GunFirePoint.Empty:Clone()
			firesound.Parent = script.Parent.Handle
			firesound:Play()
			script.Parent.Handle.GunMuzzlePoint.FakeFlash:Emit(500)
			game:GetService("Debris"):addItem(firesound,4)
			mouse.Button1Up:Connect(function()
				firing = false
			end)
			mouse.TargetFilter = workspace.BulletStorage
			script.Parent.StartFire:FireServer(mouse.Hit.p)
			task.wait(0.1) -- the delay won't work
		end
	end
end)

To be more precise, it works. But if you turn on the autoclicker or press it quickly, it does not work.

The way I see is you need a classic if cooldown == false then. Simply putting a wait for a cooldown wont work.

If you mean this

mouse.TargetFilter = workspace.BulletStorage
script.Parent.StartFire:FireServer(mouse.Hit.p)
task.wait(0.1) -- the delay won't work
firing = false

I’m tried using this. And this wont work

Then I have no clue to be honest.

mouse.Button1Up:Connect(function()
	firing = false
end)

The task.wait(0.1) needs to go inside here.

No. This is responsible for ensuring that if the player releases the mouse button, it stops shooting.
The game will just hang and you will get an error
Script timeout: exhausted allowed execution time

I meant that it would go inside there as well, above the firing = false line.

This is not how I’d personally go about writing a script for shooting however.

This doesn’t work either. Most likely because of mouse.Button1Down and all delays are ignored when I press the button again. I have an idea how to fix it.

Still haven’t figured it out, does anyone have any suggestions? :face_with_monocle:

Yay, fixed this. Just used bindable event for two local scripts. And added debounce in second local script that works!

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