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