Maximum event re-entrancy depth exceeded for Instance.AttributeChanged

Hello! I’m having an issue with my script where I need to check for if an attribute changes but whenever any attribute changes it will create this error and then crash the server. Does anyone know what this error means?

Error :
Maximum event re-entrancy depth exceeded for Instance.AttributeChanged

Code Snippet of my function :

Monster.AttributeChanged:Connect(function(AttributeName)
	if Monster:GetAttribute("AntiStun") == true and AttributeName == "Hit" and Monster:GetAttribute("Hit") ~= 0 then
		Monster:SetAttribute("Hit",0)
	end
end)

Any help would be appreciated, thank you in advance

You need to use a debounce; your code is changing the value of the attribute within the changed signal, this is causing an infinite loop

How would I go about this? I have tried using a simple debounce with a boolean variable and still am encountering the same issue.

local Debounce = false
Monster.AttributeChanged:Connect(function(AttributeName)
if Debounce ==false then
	if Monster:GetAttribute("AntiStun") == true and AttributeName == "Hit" and Monster:GetAttribute("Hit") ~= 0 then
		Monster:SetAttribute("Hit",0)
               Debounce = true
             task.wait(0.1)
               Debounce = false
	end
end
end)
local Debounce = false
Monster.AttributeChanged:Connect(function(AttributeName)
if Debounce then return end -- Stop running the thread
if Monster:GetAttribute("AntiStun") == true and AttributeName == "Hit" and Monster:GetAttribute("Hit") ~= 0 then
   Debounce = true -- set it *before* you set the new value
   Monster:SetAttribute("Hit",0)
   task.wait(0.1)
   Debounce = false
end
end)

could also do smth like this

local con
function AttributeChanged()
	if Monster:GetAttribute("AntiStun") == true and AttributeName == "Hit" and Monster:GetAttribute("Hit") ~= 0 then
        con:Disconnect()
		Monster:SetAttribute("Hit",0)
       con = Monster.AttributeChanged:Connect(AttributeChanged)
	end
end
con = Monster.AttributeChanged:Connect(AttributeChanged)

How would I go about Reconnecting this after a cooldown so it can be used again? From what I can see it only disconnects.

this line here is reconnecting it

con = Monster.AttributeChanged:Connect(AttributeChanged)