Particle emits too many times

I created a script that works when you touched the part, ( CanTouch should be set to true), and it creates a parts with emitters that should emit only ONCE.
here is the code:

image

Whenever i touch the part emitter emits a lot of times and i have no idea why. I have tried to change the script a lot of times but i still can’t figure out the problem.

1 Like

Could you show us an example of what the intended result looks like, versus what is actually happening? Just so it’s easier to gauge where the problem lies.

1 Like

You’re not disabling CanTouch.

-- in the onCanTouch function
trigger.CanTouch = false;
task.delay(cooldown, function()
   trigger.CanTouch = true;
end);
2 Likes

wow, it really helped, thanks man! <3

Do i always have to turn off funtion after firing it?

Yes otherwise it will get spammed as there’s more than 1 part in a character.

when i added this
image

my part started firing the script on touch, even if the Cantouch on start is set to false. Am I tripping or am i clearly blind?

Where did you add my code in the function? I’d show you where to put it but you sent an image instead of text

Also, you should use Debris and add those part instances so they don’t clog up workspace.

Debris:AddItem(Instance,TimeBeforeDestroy)

…Another edit…

If you are using the positions table as a random position picker you can also simply take one position and use math.random() to add or remove position from x,y,z

e.g:

Instance.Position += Vector3.new(math.random(-5,5)/10,math.random(-5,5)/10,math.random(-5,5)/10)
--math.random(-5,5)/10 returns a number from -.5 to .5

P.S. you don’t need to disable the ParticleEmitter after using :Emit() lol

does the wait(cooldown) really stop the function for 5 seconds?

"local trigger = script.Parent
local particle = game.Workspace.DEPW:WaitForChild(“ParticleEmitter”)
local childName = “pos”
local parent = workspace
local children = parent:GetChildren()

local positions = {}

for _, child in ipairs(children) do
if child.Name == childName then
table.insert(positions, child.Position)
end
end

local cooldown = 5

local function onCanTouch()
if trigger.CanTouch then
for _, position in ipairs(positions) do
local part = Instance.new(“Part”)
part.Transparency = 1
part.Anchored = true
part.Parent = workspace
part.Position = position

		local emitter = particle:Clone()
		emitter.Parent = part
		emitter:Emit(1)
		emitter.Enabled = false
	end
	trigger.CanTouch = false
	task.wait(cooldown, function()
		trigger.CanTouch = true
	end)
end

end
trigger.Touched:Connect(onCanTouch)"

The code seems correct so far, i’m not sure why it’s firing when CanTouch is false, it’s impossible. Maybe you connected the onCanTouch function on another part?