function PlayerManager.CheckForGamepass(player)
local gold = player.leaderstats.Gold
local oldValue = gold.Value
local conn
local function CheckForMultiplier(newValue)
if player:GetAttribute("DoubleGold") then
if newValue > oldValue then
local increasedAmount = newValue - oldValue
increasedAmount *= 2
gold.Value += increasedAmount
oldValue = gold.Value
conn:Disconnect()
conn = gold.Changed:Connect(CheckForMultiplier)
end
end
end
conn = gold.Changed:Connect(CheckForMultiplier)
end
I’m currently finding the difference in the newValue - oldValue and then multiplying it by 2, are there any issues from this code or am I doing it correctly?
I thought that I would need to reconnect the connection after disconnecting it. I removed it now. I tried out the code however, I got the error Maximum event re-entrancy depth exceeded for IntValue.Changed. I assumed disconnecting the event would fix it but what is causing it to perform recursion?
yes you will need to reconnect it after disconnecting if you want to use it,
but you wont need to disconnect at all
events can fire multiple times with 1 function
I figured it out. Rather than disconnecting the event as you mentioned, I used a debounce instead and it’s stopping the recursion from occurring. I also added an if statement to set the oldValue to the newValue if the value decreased to ensure that this multiplier works after a decrease in value. Thank you for the brainstorming and help!
function PlayerManager.CheckForGamepass(player)
local gold = player.leaderstats.Gold
local oldValue = gold.Value
local debounce = false
local function CheckForMultiplier(newValue)
if player:GetAttribute("DoubleGold") then
if newValue > oldValue then
if not debounce then
debounce = true
local increasedAmount = newValue - oldValue
increasedAmount *= 2
gold.Value += increasedAmount
oldValue = gold.Value
debounce = false
end
else
oldValue = newValue
end
end
end
gold.Changed:Connect(CheckForMultiplier)
end