So, radius is a brick that represents the area of effect, and I wanted it so that anyone touching radius gets an attack decrease of -0.2 ONCE. This is what i came up with
1 | radius.Touched:connect( function (hit) |
2 | local attack = hit.Parent:WaitForChild( "Attack" ) |
3 | attack.Value = attack.Value - 0.2 |
4 | end ) |
The problem is, the attack.Value - 0.2 just gets spammed by anyone moving within the radius. Is there a way so that each time someone touches the radius, the attack.Value-0.2 occurs only once, and not just spammed? ty for the help in advance
When you create an event, it returns a Connection userdata
one of the methods this disconnect
1 | local connection |
2 | connection = radius.Touched:connect( function (hit) |
3 | local attack = hit.Parent:WaitForChild( "Attack" ) |
4 | attack.Value = attack.Value - 0.2 |
5 | connection:disconnect() |
6 | end ) |
Of course, this disconnects the event and it will not trigger again.
Another idea is debounce
wiki provides a better explanation, here is an example
01 | local deb = false |
02 |
03 | radius.Touched:connect( function (hit) |
04 | if (deb) then return end |
05 | deb = true |
06 | local attack = hit.Parent:WaitForChild( "Attack" ) |
07 | attack.Value = attack.Value - 0.2 |
08 | wait( 2 ) |
09 | deb = false |
10 | end ) |