Cancel wait function

Here is the code, how do I cancel that wait function when the player shoots again so that it waits 3 seconds again after shooting, kinda like resetting and running a timer when the player shoots?

UIS.InputBegan:Connect(function(input, gameProcessed)
	if input.UserInputType == Enum.UserInputType.MouseButton1 and not gameProcessed then
		MouseDown = true
		if Equipped == true then
			coroutine.wrap(ZoomIn)(nil, Mouse2Down)
			
			
			if not isFiring then
				isFiring = true
				if AutomaticVal.Value == true then
					repeat
						FireWeapon()
						wait(Fire_Rate.Value)
					until MouseDown == false
				else
					FireWeapon()
					wait(Fire_Rate.Value)
				end
				isFiring = false
				
				wait(3) -- Cancel when player shoots again
				
				ZoomOut()
			end
		end
	end
end)

UIS.InputEnded:Connect(function(input, gameProcessed)
	if input.UserInputType == Enum.UserInputType.MouseButton1 and not gameProcessed then
		MouseDown = false
	end
end)

1 Like

I don’t think there’s a way maybe create a loop that will stop when the player shoots again?

1 Like

I tried, however it only checks the ‘until’ part of the loop after the 3 seconds has passed in the loop

UIS.InputBegan:Connect(function(input, gameProcessed)
	if input.UserInputType == Enum.UserInputType.MouseButton1 and not gameProcessed then
		MouseDown = true
		if Equipped == true then
			coroutine.wrap(ZoomIn)(nil, Mouse2Down)
		
			if not isFiring then
				isFiring = true
				if AutomaticVal.Value == true then
					repeat
						FireWeapon()
						wait(Fire_Rate.Value)
					until MouseDown == false
				else
					FireWeapon()
					wait(Fire_Rate.Value)
				end
				isFiring = false
				
				repeat
					wait(3)
					ZoomOut()
				until isFiring == true or Zoomed == false
				
				print("Ended")
			end
		end
	end
end)
1 Like

Maybe?

UIS.InputBegan:Connect(function(input, gameProcessed)
	if input.UserInputType == Enum.UserInputType.MouseButton1 and not gameProcessed then
		MouseDown = true
		if Equipped == true then
			coroutine.wrap(ZoomIn)(nil, Mouse2Down)
		
			if not isFiring then
				isFiring = true
				if AutomaticVal.Value == true then
					repeat
						FireWeapon()
						wait(Fire_Rate.Value)
					until MouseDown == false
				else
					FireWeapon()
					wait(Fire_Rate.Value)
				end
				isFiring = false
				local SecondsPassed = 0
				repeat
					wait(0.1)
                    SecondsPassed += 0.1
					ZoomOut()
				until isFiring == true or Zoomed == false or SecondsPassed >= 3
				
				print("Ended")
			end
		end
	end
end)
1 Like

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