Why are touch function is running everytime

function touched()
	local exp = Instance.new("Explosion")
	exp.Parent = script.Parent
	exp.Position = script.Parent
end

script.Parent.Touched:Connect(touched)

it suppose to be create explosion 1 times, after script.parent touch
not creating bezillion amount of explosion that rage my computer :sob:

how do i make it only create new instace of explosion only 1x

3 Likes

the easiest way to fix that would be:

local Created = false

function touched()
	if Created == false then
		Created = true
		local exp = Instance.new("Explosion")
		exp.Parent = script.Parent
		exp.Position = script.Parent
	end
end

You need a debounce!

local deb = false
local cd = .5 --you can expiriment with different values for your cooldown and see what works best

function touched()
        if deb then
        return
        end

	local exp = Instance.new("Explosion")
	exp.Parent = script.Parent
	exp.Position = script.Parent

        deb = true
        task.wait(cd)
        deb = false
end
2 Likes

now it just wont explode :v
yea so original idea was

function touched()
	local exp = Instance.new("Explosion")
	exp.Parent = script.Parent
	exp.Position = script.Parent
        wait(1)
        exp:destroy()
end

script.Parent.Touched:Connect(touched)

just didnt work

2 Likes

The code you mentioned might not be doing anything because you aren’t filtering touches. If you want it to explode when a character touches it, you have to check if what you touched is parented to a character. Or if you want it to just explode when it touches a solid surface, check if it’s CanCollide==true.

Also, to avoid exploding multiple times on the same object, add the following:

local AlreadyTouched = {}

local function touched(touch)
    -- Check if already touched
    if AlreadyTouched[touch] then return end
    AlreadyTouched[touch] = true;
    -- Get rid of memory leaks
    touch.Destroying:Connect(function()
        AlreadyTouched[touch] = nil
    end)
    -- your code here
end
2 Likes

This is happening because Part.Touched fires every heartbeat that the part is touching something else. To prevent this, like the others have suggested, you need to implement a debounce to ensure it only creates the explosion once. Here’s a more simple example.

local debounce = false

function touched()
  if debounce == true then
    return
  end -- returns the function if the debounce is set to true
  debounce = true -- set debounce to true so that this function will only run once
  -- rest of your code here
end
2 Likes

i do most of them but ended up Explosion is not working

local debounce = false

function touched()
	if debounce == true then
		return
	end -- returns the function if the debounce is set to true
	debounce = true -- set debounce to true so that this function will only run once
	explosion = Instance.new("Explosion")
	explosion.Parent = script.Parent
	explosion.Position = script.Parent.Position
	wait(.5)
	script.Parent:Destroy()
end
1 Like

im assuming you want the part to explode everytime it get touched by a player yeah?

function touched(Touch)
	local Humanoid = Touch.Parent:FindFirstChild("Humanoid")
	if Humanoid and Humanoid.Health > 0 then
		local exp = Instance.new("Explosion")
		exp.Parent = script.Parent
		exp.Position = script.Parent
	end
end

script.Parent.Touched:Connect(function(Touch)
	touched(Touch)
end)

let me know if this works

1 Like
function touched()
	local exp = Instance.new("Explosion")
	exp.Parent = script.Parent
	exp.Position = script.Parent
end

script.Parent.Touched:Once(touched)
2 Likes