In my game, I am working on adding a Poison status effect, I have two while loops doing two different things; one is counting down the venom value when it is more than 0, and the other is removing the player's health as long as the value is above 0. The problem is, the loops start doing their function multiple times. For example, the while loop that is supposed to be counting the venom value down while it is above 0 will work fine for the first loop, but then the loop starts stalking on top of itself, and won't even stop when it is at 0. Instead, it counts down like this: 10, 9, 7, 3, -5, and then stops at -5. I have no idea why this is happening, help would be appreciated.
char.charactervalues.Venom.Changed:Connect(function() if char.charactervalues.Venom.Value > 0 then char.Torso.Poison.Enabled = true while char.charactervalues.Venom.Value ~= 0 do char.Humanoid.Health -= 3 wait(0.8) end else char.Torso.Poison.Enabled = false end end) char.charactervalues.Venom.Changed:Connect(function() if char.charactervalues.Venom.Value > 0 then while char.charactervalues.Venom.Value ~= 0 do wait(1) char.charactervalues.Venom.Value -= 1 end end end)
The way :Connect() functions work, is that each time the event triggers (in this case, the venom value), it starts a new function running the code. So, when your while loop is running, it is changing the venom value, and stacking new loops on top, causing it to behave very weirdly. A simple fix for this is a debounce value. Changed code:
debounce = false char.charactervalues.Venom.Changed:Connect(function() if debounce == false then debounce = true if char.charactervalues.Venom.Value > 0 then char.Torso.Poison.Enabled = true while char.charactervalues.Venom.Value >= 0 do char.Humanoid.Health -= 3 wait(0.8) end else char.Torso.Poison.Enabled = false end debounce = false end end) char.charactervalues.Venom.Changed:Connect(function() if debounce == false then if char.charactervalues.Venom.Value > 0 then while char.charactervalues.Venom.Value >= 0 do wait(1) char.charactervalues.Venom.Value -= 1 end end end end)