Maximum event re-entrancy depth exceeded for IntValue.Changed

Hey is there a way to fix this error?

Let’s say for example; A level 1 character.
image

And this character is given a big amount of EXP:
https://gyazo.com/43fbcb50d8c503a499c49ed9966edbf3

If you look at the gif you see in output, “Maximum event re-entrancy depth exceeded for IntValue.Changed”

Hopefully this can help:

Stats.Characters.ChildAdded:Connect(function(v) --Characters XP
		coroutine.resume(coroutine.create(function()
			for _, Stat in pairs(v:GetChildren()) do
				if Stat.Name == "XP" then
					local XPStat = Stat
					XPStat.Changed:Connect(function()
						CharacterXPChanging(Player, XPStat, XPStat.Parent.Level, XPStat.Parent.Refined)
						if Stat.Parent.Level.Value >= BaseCharacterLevel + (Stat.Parent.Refined.Value * RefineScaler) then
							Stat.Parent.Level.Value = BaseCharacterLevel + (Stat.Parent.Refined.Value * RefineScaler)
						end
						print("added exp here")
					end)
				end
			end
		end))
	end)
function CharacterXPChanging(Player, XP, Level, Refined)
	local XPCap = Level.Value * CharacterLevelMultiplier
	if XP.Value >= XPCap then
		if Level.Value <= BaseCharacterLevel + (Refined.Value * RefineScaler) then 
			Level.Value = Level.Value + 1
			XP.Value = XP.Value - XPCap
			TaskModule.DailyTasks(Player, "LevelTask", 1)

			if Level.Value >= BaseCharacterLevel + (Refined.Value * RefineScaler) then
				Level.Value = BaseCharacterLevel + (Refined.Value * RefineScaler)
			end
			
		end
	end
end```

This here creates a loop because, once you change the Stat in the .Changed event, it also activates the .Changed event because the .Changed event fires everytime the value is changed, you should add a debounce/cooldown so it doesn’t repeat itself infinitely.

Thank you,

a debounce then calling the function again (so they can level up multiple times if they get a huge amount of xp) seems to be playing out good.

local FeedDB = {}
function CharacterXPChanging(Player, XP, Level, Refined)
	local XPCap = Level.Value * CharacterLevelMultiplier
	if XP.Value >= XPCap then
		if Level.Value <= BaseCharacterLevel + (Refined.Value * RefineScaler) and not FeedDB[Player] then
			FeedDB[Player] = true
			Level.Value = Level.Value + 1
			XP.Value = XP.Value - XPCap
			TaskModule.DailyTasks(Player, "LevelTask", 1)
			if Level.Value >= BaseCharacterLevel + (Refined.Value * RefineScaler) then
				Level.Value = BaseCharacterLevel + (Refined.Value * RefineScaler)
			end
			FeedDB[Player] = false
			coroutine.resume(coroutine.create(function()
				CharacterXPChanging(Player, XP, Level, Refined)
			end))
		end
	end
end
1 Like