Maximum event re-entrancy depth exceeded for NumberValue.Changed

I get the following error when adding a number to the value

image

Script:
local Value = 0.25

	Cash.Changed:Connect(function()
		if Crytal1.Value == true then
			Cash.Value = Cash.Value+Value
			print(Cash.Value)
		end
	end)

I added 1 to the value so I would have to add: 1 + 0.25

The result should be: 1.25 but it is added more times and gives 2.5 since it is added 6 times

I am using “NumberValue”, since “IntValue” does not allow me to display the numbers after the “.”

If anyone can help me I will be very grateful!

2 Likes

You connected an event that detects when the Value is changed and changed the value within it creating a loop

2 Likes

Cash.Values state is being changed locally on the stack but you are getting the global thingy making the computer cry

2 Likes

This is happening because your changing the value of Cash within the Changed function, thereby instantly repeating the function indefinitely. The error your seeing is stopping your game from crashing with a runtime error (infinite loop).

You should set Crytal1’s value to false after adding value to Cash, or some other way to prevent the infinite loop.

3 Likes

Crystal1’s value is only activated once when clicking on a button, I really don’t know how to stop that “Loop”

if you could help me it would be great :slight_smile:

And how can I break that loop?

what @MayorGnarwhal suggested
This would make the next function that would be called stop because the value would be false

local Value = 0.25
local expected = -1

	Cash.Changed:Connect(function()
		if Crytal1.Value == true and Cash.Value ~= expected then
            expected = Cash.Value + Value
			Cash.Value = Cash.Value+Value
			print(Cash.Value)
		end
	end)
1 Like

Well what I’m confused about is why you need the Changed event anyways? Where are you changing the value of Cash to even trigger this event? Changed is called when the value changes, either increasing or decreasing, so there’s no reason to want to further change that value after it has already been changed.

Judging by your code, I’m assuming you want a bonus 0.25 cash when Crytal1 is true? If so, you can remove the Changed event entirely and when you add to Cash do something like this,

Cash.Value = Cash.Value + Value + (Crytal1.Value and 0.25 or 0)
2 Likes

what I want to do is that every time the “Cash” value changes it detects if it has a crystal that crystal will give you extra money thats why

You’d need to either have a debounce or disconnect your connection then.


With a debounce:

local debounce = false
Cash.Changed:Connect(function()
	if debounce then
		debounce = false
		return
	end
	if Crytal1.Value == true then
		debounce = true
		Cash.Value = Cash.Value+Value
		print(Cash.Value)
	end
end)

With disconnecting a connection:

local connection
local function onCashChanged()
	if Crytal1.Value == true then
		connection:Disconnect()
		Cash.Value = Cash.Value+Value
		print(Cash.Value)
		Cash.Changed:Wait()
		connection = Cash.Changed:Connect(onCashChanged)
	end
end

connection = Cash.Changed:Connect(onCashChanged)

Note that in both cases Value is the bonus cash, Cash is an IntValue or a NumberValue, and Crystal1 is a BooleanValue

5 Likes

Okay, well that’s what I assumed in my last message, so you can remove the Changed event add when you add to Cash use this:

I also imagine you will add multiple Crystals, so it would be easier to be able to put all the Crystal values together (such as in a folder) and be able to loop through them to add to the value.

local CrystalFolder = -- folder that holds all Crystal truth values

Cash.Value = Cash.Value + Value -- add base value

for i, crystal in pairs(CrystalFolder:GetChildren()) do -- loop through Crystals
    if crystal.Value then
        Cash.Value = Cash.Value + BonusValue -- add extra if crystal is true
    end
end)