I want to make a hitbox that changes a variable

I am making a game where you have anxiety 0.5/s but when you go into the light (a hitbox) that is anchored and uncollidable it decreases 0.25/s. But the event just refuses to work with no errors

-- AnxietyTrigger.lua (LocalScript or Script depending on design)

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local AnxietyManager = require(ReplicatedStorage:WaitForChild("AnxietyManager"))

local Workspace = game:GetService("Workspace")
local RunService = game:GetService("RunService")

local Part = script.Parent
Part.CanCollide = false
Part.CanTouch = true

-- Optional visual/audio feedback
-- Part.Transparency = 1  -- Uncomment to make invisible
-- Part.CanQuery = false -- Optional: exclude from raycasts

local debounce = false
local cooldown = 1 -- seconds between triggers

RunService.Heartbeat:Connect(function()
	if debounce then return end

	local touchingParts = Workspace:GetPartsInPart(Part)
	for _, hit in ipairs(touchingParts) do
		local character = hit:FindFirstAncestorOfClass("Model")
		if character and character:FindFirstChild("Humanoid") then
			debounce = true
			print("Anxiety triggered by:", character.Name)
			AnxietyManager.RemovePercentage(0.25)

			-- Optional: Sound/particle effect
			-- ReplicatedStorage.TriggerAnxietyFX:FireClient(game.Players:GetPlayerFromCharacter(character))

			task.wait(cooldown)
			debounce = false
			break
		end
	end
end)

If you are running this in LocalScript, inside part/object in workspace, then this won’t run at all. Only Scripts work in workspace, so I suggest you change to script from localscript.

this is going to throttle runservice.

you are getting parts in a region (performance depends on how big this area is) then iterating through them all with a wait.

runservice is not meant for slow loops such as these. its meant for near-instant changes to data.

i’d recommend wrapping the loop into a task.defer so it solves outside of heartbeat.