Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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

1 comment

0

do you want to disconnect it?

1 answer

✓ Accepted answer

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)

2 comments

0

ty for the idea! it worked :)

This discussion is preserved as it was. New answers, comments, and votes are closed.