How would I make my script not repeatdl'y fire?

This script works PERFECTLY fine, yet I feel there has to be a different / more professional way of this? Instead of settings a value to true and false and then waiting, yet if there is not this is perfectly fine for me.

Script :

local HorrorFOV = game:GetService("ReplicatedStorage").HorrorFOV 
local RegularFOV = game:GetService("ReplicatedStorage").RegularFOV
local Detector = script.Parent
local horrorfovactive = true

Detector.Touched:Connect(function(hit)
	local player = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent)
	if player and horrorfovactive then
		print("Player touched part, firing "..HorrorFOV.Name.."!")
		HorrorFOV:FireClient(player)
		print(HorrorFOV.Name.." has been fired!")
		horrorfovactive = false
		wait(3)
		horrorfovactive = true
	end
end)

Thank you for any contribution.

Is that meant to act as a debounce? Because that’s what it looks like.

2 Likes

Well, the only recommendation I can give you is to put horrorfovactive = false under the check on whether the player exists and if the horrorfovactive variable is true. Other than that, this is basically the most efficient way to do it. Debounces are a very common way for developers to add delay and there isn’t really a more efficient way (there are multiple other ways, just not efficient). I also recommend turning wait(3) to task.wait(3).

3 Likes

For me, I would have a return function when horrorFovActive == false. It makes it readable, at least for me. I would also have horrorFovActive = false set from the beginning to see where that debounce is located in case your script gets really big.