Help with debounce on this script

Idk where to put a debouce on this script, the script detects when an npc is on screen and offscreen.
But right now it makes a lot of lag
Any help is appreciated

local Camera = game.Workspace.CurrentCamera
local Npcs = game.Workspace:WaitForChild("Npcs")
local Npc = Npcs:WaitForChild("Npc1")
game:GetService('RunService').Stepped:Connect(function()
	local Position, PlayerCanSee = Camera:WorldToViewportPoint(Npc:WaitForChild("HumanoidRootPart",nil).Position)
	if PlayerCanSee then
		game.ReplicatedStorage.EnemyRemotes.Npcs.OnScreen:FireServer()
	else
		game.ReplicatedStorage.EnemyRemotes.Npcs.OffScreen:FireServer()
	end
end)
local Camera = game.Workspace.CurrentCamera
local Npcs = game.Workspace:WaitForChild("Npcs")
local Npc = Npcs:WaitForChild("Npc1")
local debounce = false

game:GetService('RunService').Stepped:Connect(function()
	if not debounce then  -- Check if the debounce flag is false
		local Position, PlayerCanSee = Camera:WorldToViewportPoint(Npc:WaitForChild("HumanoidRootPart", nil).Position)
		if PlayerCanSee then
			game.ReplicatedStorage.EnemyRemotes.Npcs.OnScreen:FireServer()
		else
			game.ReplicatedStorage.EnemyRemotes.Npcs.OffScreen:FireServer()
		end
		debounce = true
		task.wait(1)  -- Wait for a certain duration (adjust as needed)
		debounce = false
	end
end)

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