So what this code about is if the resources goes above the max res, for example 300 (from other resources) + 60 (the resources that goes above the max) /320 (the max resources) then the resources that goes above the max will be reduced using this line of code: res.Value = (prevTotalRes + res.Value) - plr.MaxRes.Value but the problem is that the code triggers the .Changed function so it causes a loop, so im asking for help if theres a solution. The full code:
for i, res in pairs(plr.Resources:GetChildren()) do
if not res:FindFirstChild("FrontFrame") then
local prevRes = 0
local prevTotalRes = 0
res.Changed:Connect(function(val)
prevTotalRes = totalRes.Value
totalRes.Value += (val - prevRes)
prevRes = val
if totalRes.Value >= plr.MaxRes.Value then
totalRes.Value = plr.MaxRes.Value
res.Value = (prevTotalRes + res.Value) - plr.MaxRes.Value
end
end)
end
end
Try adding a debounce mechanism so it prevents running the code that changes the value again inside the event.
for i, res in pairs(plr.Resources:GetChildren()) do
if not res:FindFirstChild("FrontFrame") then
local prevRes = 0
local prevTotalRes = 0
local debounce = false
res.Changed:Connect(function(val)
if debounce then
return -- value is being changed, stop the function here
end
debounce = true
prevTotalRes = totalRes.Value
totalRes.Value += (val - prevRes)
prevRes = val
if totalRes.Value >= plr.MaxRes.Value then
totalRes.Value = plr.MaxRes.Value
res.Value = (prevTotalRes + res.Value) - plr.MaxRes.Value
end
debounce = false
end)
end
end
That technique no longer works with the deferred signal behaviour, (see workspace.SignalBehaviour)
Workaround I found is to put the debounce = false inside of two task.defer()…
Other workaround is to set workspace.SignalBehaviour to immediate (cuz deferred is stupid, thanks roblox)
I have yet to know of an actual solution to this that isn’t a really stupid workaround