Touched even firing multiple times

Hello, i have this script, and for some reason, the Debris for the explosion and fire fires multiple times and i only want it to do it once

bullet.Touched:Connect(function(hit)
        if hit.Name == "Terrain" then
            
            local FireDebris = game.ReplicatedStorage.FireRelease.FireDebris:Clone() --fire
            FireDebris.Parent = workspace
            FireDebris.CFrame = bullet.CFrame
            FireDebris.Anchored = true
            Debris:AddItem(FireDebris, 5)
            local Explosion = game.ReplicatedStorage.FireRelease.ExplosionParticle:Clone() --/explosion
            Explosion.Parent = workspace
            Explosion.CFrame = bullet.CFrame
            Explosion.Anchored = true
            Explosion.ParticleEmitter:Emit(1)
            Debris:AddItem(Explosion, 1)
            bullet:Destroy()
            print("hitterraim")
        elseif hit:IsA("BasePart") then
            local FireDebris = game.ReplicatedStorage.FireRelease.FireDebris:Clone()
            FireDebris.Parent = workspace
            FireDebris.CFrame = bullet.CFrame
            FireDebris.Anchored = true
            Debris:AddItem(FireDebris, 5)
            bullet:Destroy()
        end
local conn
conn =  bullet.Touched:Connect(function(hit)

when the bullet touches smth you do conn:Disconnect inside the if statements.

1 Like

Uhhh, how would the exactly work?

1 Like

you should probably implement this in the script

This makes so that the touch event doesnt fire multiple times. Put the conn:Disconnect()
below both bullet:Destroy() lines.

I dont know if its just me, but it doesnt seem that efficient.

I also have no idea how this works

1 Like

The CanTouch property just makes if the part’s .Touched event works or not. (if CanTouch is true then the part’s .Touched event will work, if CanTouch is not true then the part’s .Touched event wont work.) btw correct me if i got something wrong

try this

bullet.Touched:Connect(function(hit)
bullet.CanTouch = false
        if hit.Name == "Terrain" then
            
            local FireDebris = game.ReplicatedStorage.FireRelease.FireDebris:Clone() --fire
            FireDebris.Parent = workspace
            FireDebris.CFrame = bullet.CFrame
            FireDebris.Anchored = true
            Debris:AddItem(FireDebris, 5)
            local Explosion = game.ReplicatedStorage.FireRelease.ExplosionParticle:Clone() --/explosion
            Explosion.Parent = workspace
            Explosion.CFrame = bullet.CFrame
            Explosion.Anchored = true
            Explosion.ParticleEmitter:Emit(1)
            Debris:AddItem(Explosion, 1)
            bullet:Destroy()
            print("hitterraim")
        elseif hit:IsA("BasePart") then
            local FireDebris = game.ReplicatedStorage.FireRelease.FireDebris:Clone()
            FireDebris.Parent = workspace
            FireDebris.CFrame = bullet.CFrame
            FireDebris.Anchored = true
            Debris:AddItem(FireDebris, 5)
            bullet:Destroy()
        end

Thats one of the most simple and effective ways try it, trust me, the CanTouch method also works.

local connection
connection = bullet.Touched:Connect(function(hit)
    if hit.Name == "Terrain" then
        -- Stuff
        connection:Disconnect()
        elseif hit:IsA("BasePart") then
        -- Stuff
        connection:Disconnect()
    end
end)

Wait… so basically this is the replacement for the “conn:Disconnect()” method? Thanks bro

yeah, i did try it, didnt work

1 Like

Show me the output, also you didn’t give much details about the script itself and what it is supposed to do.

Oh yeah, sorry about that, i eneded up finding out taht it was a problem with my particle not the script.

This is no longer the easiest implementation of this method; one can now use :Once instead of :Connect.
But then it will never work again, which may not be intended behavior. I would recommend a debounce (so using CanTouch = true/false…). The debounce can also be applied per touched part using dictionaries, if you want a player not to block the touch event for another player for example.

1 Like

Add a debounce asasasasaasasa saa

here have a debounce

local Debounce = false
urpart.Touched:Connect(function()
    if not Debounce then
        Debounce=true
        --urcodehere
    end
end)

Surprisingly only the last 3 posts mention using a debounce, but yea a debounce should work for you

local debounce = false
bullet.Touched:Connect(function(hit)
    if debounce == false then
        debounce = true
        if hit.Name == "Terrain" then
            
            local FireDebris = game.ReplicatedStorage.FireRelease.FireDebris:Clone() --fire
            FireDebris.Parent = workspace
            FireDebris.CFrame = bullet.CFrame
            FireDebris.Anchored = true
            Debris:AddItem(FireDebris, 5)
            local Explosion = game.ReplicatedStorage.FireRelease.ExplosionParticle:Clone() --/explosion
            Explosion.Parent = workspace
            Explosion.CFrame = bullet.CFrame
            Explosion.Anchored = true
            Explosion.ParticleEmitter:Emit(1)
            Debris:AddItem(Explosion, 1)
            bullet:Destroy()
            print("hitterraim")
        elseif hit:IsA("BasePart") then
            local FireDebris = game.ReplicatedStorage.FireRelease.FireDebris:Clone()
            FireDebris.Parent = workspace
            FireDebris.CFrame = bullet.CFrame
            FireDebris.Anchored = true
            Debris:AddItem(FireDebris, 5)
            bullet:Destroy()
        end
    end
end)
1 Like

Oh i didnt know about that, thanks for the information, also, i said “one of” the most simple and effective ways, not the most simple and effective way