Hi, So I’m trying to make it so when the Amount of Money you have Changes, it multiplies it by .25 or 1 / 4
I did this equation: 100 + (100 * (1/4)) which gives me 125, basically what I want
(I’m not the smartest to Idk)
And while doing this, the game for some reason fires .Changed everytime after doing this, which gives me a giant number that ends up breaking .Changed.
Giving me this error: Maximum event re-entrancy depth exceeded for NumberValue.Changed
Cash.Changed:Connect(function(newReb)
if Rebirth.Value > 0 then
Cash.Value = math.floor(Cash.Value + (Cash.Value * (Rebirth.Value / 4)))
profile.Data.money = Cash.Value
else
profile.Data.money = newReb
end
I would suggest using a debounce or disconnect it from the function entirely to prevent the error from happening.
When you apply the value to Cashwithin the function, you’re essentially creating a loop because you’re firing that event again every time the event fires.
This should solve the issue:
local db = false
Cash.Changed:Connect(function(newReb)
if db == false then
db = true
if Rebirth.Value > 0 then
Cash.Value = math.floor(Cash.Value + (Cash.Value * (Rebirth.Value / 4)))
profile.Data.money = Cash.Value
else
profile.Data.money = newReb
end
db = false
end
end)
Also,
Instead of using 100 + (100 * (1/4)), why not use 100+100*.25?
That would just make the Effect go slower rather than stopping it
Thats basically an Example of what Im doing in the code
Because I’m Multiplying a Integer that will continue to add up, when +1 is added to the rebirths, it also adds an extra .25 so then 2 / 4 is .5, also i want to prioritize the Multiplication.
I’m confused…
The debounce will prevent the function from running again, (causing a loop-effect) assuming your goal is for the code under Cash.Changed:Connect(function(newReb) to run every time the event is fired.
I was about to solve this one for you, but it seems like @TwoMadDev got there before me.
local oldValue = Cash.Value
Cash.Changed:Connect(function(newReb)
if Cash.Value == oldValue then return end
if Rebirth.Value > 0 then
Cash.Value = math.floor(Cash.Value + (Cash.Value * (Rebirth.Value / 4)))
profile.Data.money = Cash.Value
else
profile.Data.money = newReb
end
oldValue = Cash.Value
end)
Saving the current value as a variable and checking whether it’s the same or not, if it’s the same then stop the function, else, run it normally and re-set the oldValue
local function UpdateCash(NewValue)
if Rebirth.Value > 0 then
Cash.Value *= 0.25*Rebirth.Value+1
Cash.Value = math.floor(Cash.Value)
profile.Data.money = Cash.Value
else
profile.Data.money = newReb
end
This does the same thing as a debounce with a few extra steps. (AKA: a less efficient way of preventing the function to recursively call itself through the connected event. Do not do this.)
A debounce is the most efficient way to prevent something from happening too many times within a certain period of time.
Your method is similar in a way that it checks a value to determine whether or not the function should run at all. However, it requires you to assign a new value each time and simply overcomplicates the idea of a debounce. There is no reason to use a “custom” debounce like the one you provided.
But like @TwoMadDev stated, there’s no point in making the function recursive at all, so this should not be an issue in the first place.