How would I have a Value not be affected with Changed?

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

How should I fix this?

1 Like

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 Cash within 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.

Well you’re recursivly calling the event so idk what you were expecting.

Cash.Value *= 0.25*Rebirth.Value+1

:exploding_head:

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.

I would prolly do this…

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

1 Like
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.)

Instead of this, just make it not recurse itself at all. @DasKairo theres no point in having recursion with something like this.

1 Like

this ended up giving me .02 instead of 125 for some reason

Cash.Value = Cash.Value + (Rebirth.Value * 0.25)?

no? this isn’t worse than a debounce at all.

a debounce could be worse, did u try…?

I think you mean Rebirth.Value / 4 here.

try brackets

–Random text because of roblox 30 limit for char lol

A small note, use multiplication as it’s way faster than division, that’s how these machines work so ya.

1 Like

@bonkybincer @msix29

Both of your scripts didn’t seem to fix the Issue, still gives me the same error

1 Like

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.

1 Like

Prove that a debounce is faster.

@DasKairo, if it doesn’t need to be recursive, why do so?

Faster by .0000000001 nano seconds :joy:

1 Like

The script is still throwing errors at you because you’re still firing the event. To my knowledge there is no way to circumvent this with your method.

Again… the function should not be recursive. :man_facepalming: