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 7 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

radius.Touched:connect(function(hit)
    local attack = hit.Parent:WaitForChild("Attack")
    attack.Value = attack.Value - 0.2
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

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

1 answer

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

When you create an event, it returns a Connection userdata

one of the methods this disconnect

local connection
connection = radius.Touched:connect(function(hit)
    local attack = hit.Parent:WaitForChild("Attack")
    attack.Value = attack.Value - 0.2
    connection:disconnect()
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

local deb = false

radius.Touched:connect(function(hit)
    if (deb) then return end 
    deb = true 
    local attack = hit.Parent:WaitForChild("Attack")
    attack.Value = attack.Value - 0.2
    wait(2)
    deb = false
end)

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

Answer this question