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
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
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
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
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
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)
function touched()
local exp = Instance.new("Explosion")
exp.Parent = script.Parent
exp.Position = script.Parent
end
script.Parent.Touched:Once(touched)