Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

Is there a way to stop .Touched from being spammed?

Asked by 8 years ago

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

1radius.Touched:connect(function(hit)
2    local attack = hit.Parent:WaitForChild("Attack")
3    attack.Value = attack.Value - 0.2
4end)

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

0
do you want to disconnect it? TheLowOne 85 — 8y

1 answer

Log in to vote
1
Answered by 8 years ago
Edited 8 years ago

When you create an event, it returns a Connection userdata

one of the methods this disconnect

1local connection
2connection = radius.Touched:connect(function(hit)
3    local attack = hit.Parent:WaitForChild("Attack")
4    attack.Value = attack.Value - 0.2
5    connection:disconnect()
6end)

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

01local deb = false
02 
03radius.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
10end)

0
ty for the idea! it worked :) beatersz 25 — 8y
Ad

Answer this question