So i have been trying to make a script where if a tool touches a ParticleEmitter it will cause an explosion. The explosion part works but i only want it to make an explosion for when it touches the particle emitter. Before i say more, here is the script:
script.Parent.Touched:Connect(function(hit)
if hit:FindFirstChild("ParticleEmitter") then
script.Parent.Sound:Play()
local explosion = Instance.new("Explosion")
explosion.BlastRadius = 0
explosion.BlastPressure = 0
explosion.DestroyJointRadiusPercent = 0
explosion.Position = script.Parent.Position
explosion.Parent = game.Workspace
end
end)
It is rather frustrating as I have also tried a script like this:
function onTouched(part)
if part.Name == "ParticleEmitter" then
script.Parent.Sound:Play()
local explosion = Instance.new("Explosion")
explosion.BlastRadius = 0
explosion.BlastPressure = 0
explosion.DestroyJointRadiusPercent = 0
explosion.Position = script.Parent.Position
explosion.Parent = game.Workspace
end
end
script.Parent.Touched:connect(onTouched)
Neither of them have worked however the second script caused the explosion for every time it was touched, wether it was a ParticleEmitter or not.
I’m not asking for a new script but any help would be appreciated! If you want any more info i’ll happily give more.
By using Instance:FindFirstChild(string) you are actually looking for an instance with that name, but if you use Instance:FindFirstChildWhichIsA(class) instead, it’ll look for an instance which ClassName’s matches that.
script.Parent.Touched:Connect(function(hit)
if hit and hit:FindFirstChildWhichIsA("ParticleEmitter") then
local particle = hit:FindFirstChildWhichIsA("ParticleEmitter")
particle:Destroy()
local explosion = Instance.new("Explosion")
explosion.BlastRadius = 0
explosion.BlastPressure = 0
explosion.DestroyJointRadiusPercent = 0
explosion.Position = script.Parent.Position
explosion.Parent = workspace
end
end)
Also, I don’t know if your intention was deleting the emitter post collision, but on the code I wrote I’ve implemented that already!
Oh, and to add up onto this:
Incase you do not want a part to lose it’s particle emitter but also don’t want it to be triggered again you can use a dictionary to achieve something like a Blacklist.
local blacklist={}
script.Parent.Touched:Connect(function(hit)
if hit and not blacklist[hit] and hit:FindFirstChildWhichIsA("ParticleEmitter") then
--The "not blacklist[hit]" is just to check if the part you are touching isn't already in the blacklist.
blacklist[hit]=true --This adds the part into the blacklist, so if you ever collide on the part again utilizing the tool, it won't trigger it again. (Unless the script is resetted)
local explosion = Instance.new("Explosion")
explosion.BlastRadius = 0
explosion.BlastPressure = 0
explosion.DestroyJointRadiusPercent = 0
explosion.Position = script.Parent.Position
explosion.Parent = workspace
end
end)
Thank you all for the help but I managed figured it out myself!
I changed the script to this format:
local Value1 = 0
script.Parent.Touched:Connect(function(touch)
if touch.Name == "CopperOxideZone" and Value1 == 0 then
wait(1)
script.Parent.Sound:Play()
local explosion = Instance.new("Explosion")
explosion.BlastRadius = 0
explosion.BlastPressure = 0
explosion.DestroyJointRadiusPercent = 0
explosion.Position = script.Parent.Position
explosion.Parent = game.Workspace
Value1 = 1
touch.Parent.Union.Transparency = 1
touch.Anchored = false
end
end)
The script is now in the particle emitter so i believe it was the script being in a tool that caused the problem. I may be wrong as i did change the script format but it does work now!
Thank you everybody for helping me on my first post, i hope you all have a great day!