Okay so basically, when deeds value changed, i want it to be multipled by two, but since it also changes, .Changed event will call again thus multiplying the number even further. For example, changing deeds from 500 to intended 1000 will actually change it to a higher number due to the loop.
The loop in question is .Changed running more than intended due to this
Value.Changed:Connect(function(NewVal)
game:GetService("Players"):FindFirstChild(workspace.Moai.Moai.Owner.Value).leaderstats.Deeds.Value = NewVal * 2 -- Main culprit, causing .Changed to run again because same instance.
end)
Don’t use .Changed for this, instead do this when the Value is being changed by another script, you are basically telling the item to change while the .Changed Connection is there, because of that it will continue firing as a change happened to the object you are changing, it will keep doing this until it breaks.
This wont do anything, it will still detect the changes, and continue to loop
Yeah, I figured there is no possible solutions so I just used a function that is primary and heavily used for giving out Deeds.
function PlayerModule:Click(Player, Deed)
if workspace:FindFirstChild("Crown") then
Player:FindFirstChild("leaderstats"):FindFirstChild("Deeds").Value += Deed + Core.Values.HasCarpet:FindFirstChild("Bonus").Value * 2
else
Player:FindFirstChild("leaderstats"):FindFirstChild("Deeds").Value += Deed + Core.Values.HasCarpet:FindFirstChild("Bonus").Value
end
end
local Value = game:GetService("Players"):FindFirstChild(workspace.Moai.Moai.Owner.Value).leaderstats.Deeds
Value:GetPropertyChangedSignal("Value"):Connect(function()
game:GetService("Players"):FindFirstChild(workspace.Moai.Moai.Owner.Value).leaderstats.Deeds.Value *= 2
end)
:GetPropertyChangedSignal() will only fire when the given property changes rather than .Changed since it fires when any property of a given instance is changed
If i’m understanding your problem correctly it should solve your problem.
local Value = game:GetService("Players"):FindFirstChild(workspace.Moai.Moai.Owner.Value).leaderstats.Deeds
local debounce = false
Value.Changed:Connect(function(NewVal)
if debounce then return end
debounce = true
game:GetService("Players"):FindFirstChild(workspace.Moai.Moai.Owner.Value).leaderstats.Deeds.Value = NewVal * 2
debounce = false
end)
For deferred signal behavior:
local Value = game:GetService("Players"):FindFirstChild(workspace.Moai.Moai.Owner.Value).leaderstats.Deeds
local debounce = false
Value.Changed:Connect(function(NewVal)
if debounce then return end
debounce = true
game:GetService("Players"):FindFirstChild(workspace.Moai.Moai.Owner.Value).leaderstats.Deeds.Value = NewVal * 2
task.wait(.1)
debounce = false
end)
Why is this thread 15 posts long at this point? This was easy to solve.