Particle giver debounce

Hey, so i tried to make a particle giver part but it gives the particles multiple times and the debounce doesnt seem to work i would really appreciate if someone can fix it for me (i would really like that instead of a particle giver part its a part that gives a particle once you click preferably if its toggleable, the click can be replaced with proximity prompt but the particle giver part its still ok altough if ur doing the another ones i would really appreciate if its in a downloadable place or model, thanks.)
heres the code:

function onTouched(hit)
	local db = false
	
	local human = hit.Parent:findFirstChild("Humanoid")
	if db then return end
	db = true
		script.Parent.ParticleEmitter:clone().Parent = human.Parent:findFirstChild("Head")
		script.Parent.ParticleEmitter:clone().Parent = human.Parent:findFirstChild("Left Leg")
		script.Parent.ParticleEmitter:clone().Parent = human.Parent:findFirstChild("Left Arm")
		script.Parent.ParticleEmitter:clone().Parent = human.Parent:findFirstChild("Right Arm")
		script.Parent.ParticleEmitter:clone().Parent = human.Parent:findFirstChild("Right Leg")
		script.Parent.ParticleEmitter:clone().Parent = human.Parent:findFirstChild("Torso")
		script.Parent.ParticleEmitter2:clone().Parent = human.Parent:findFirstChild("Head")
		script.Parent.ParticleEmitter2:clone().Parent = human.Parent:findFirstChild("Left Leg")
		script.Parent.ParticleEmitter2:clone().Parent = human.Parent:findFirstChild("Left Arm")
		script.Parent.ParticleEmitter2:clone().Parent = human.Parent:findFirstChild("Right Arm")
		script.Parent.ParticleEmitter2:clone().Parent = human.Parent:findFirstChild("Right Leg")
		script.Parent.ParticleEmitter2:clone().Parent = human.Parent:findFirstChild("Torso")
			wait(2.5)
			db = false
        end
script.Parent.Touched:connect(onTouched)

sorry if its too bad, im a newbie :sweat_smile:

Few notes -

1- Instead of wait, use task.wait().
2- You could shortn all these lines with a simple for loop.
3- Try this one :


function onTouched(hit)
	local db = false
	local human = hit.Parent:findFirstChild("Humanoid")
	if db == false then 
		db = true
		for _,bodyPart in pairs(hit.Parent:GetChildren()) do
			if bodyPart:IsA("BasePart") then
				if not bodyPart:FindFirstChild("ParticleEmitter") then
				local clone = script.Parent.ParticleEmitter:Clone()
					clone.Parent = bodyPart
				end
			end
		end
		task.wait(2.5)
		db = false
	end
end
script.Parent.Touched:connect(onTouched)
1 Like

Hey, thanks for taking the time the problem its that it gives more than one particle on touch basically it looks like the debounce isn’t working

You could put a debounce in the for loop to obtain this.

1 Like
function onTouched(hit)
	local db = false
	local human = hit.Parent:findFirstChild("Humanoid")
	if db == false then 
		db = true
		for _,bodyPart in pairs(hit.Parent:GetChildren()) do
			if bodyPart:IsA("BasePart") then
				if not bodyPart:FindFirstChild("ParticleEmitter") then
				local clone = script.Parent.ParticleEmitter:Clone()
					clone.Parent = bodyPart
				end
			end
           task.wait(1.2)
		end
		task.wait(2.5)
		db = false
	end
end
script.Parent.Touched:connect(onTouched)
1 Like

This will apply the Emitter to each of your bodyparts with a 1.2 seconds difference between each.

1 Like

Just check if particle exists in body parts or not,
Also put db above the ontouch event, the debounce is setting to false whenever someone is touching the part, if its a server script then I suggest you to use db as table.

I also don’t think using wait, in a server script is a good thing, i prefer you to use delay() in touch events for debounce.

local db = {}
function onTouched(hit)
    local player = game.Players:GetPlayerFromCharacter(hit.Parent)
    if player then
      if not db[player] then
          table.insert(db,player)
          for i,part in pairs(player.Character:GetChildren()) do
               if part:IsA("BasePart") then
                     if not part:FindFirstChild("ParticleEmitter") then
                         script.Parent.ParticleEmitter:clone().Parent = part
                     end
                      if not part:FindFirstChild("ParticleEmitter2") then
                         script.Parent.ParticleEmitter2:clone().Parent = part
                     end
               end
          end
          delay(2.5,function() table.remove(db,table.find(db,player)) end)
       end
    end
end
script.Parent.Touched:connect(onTouched)

Hope this helps.

1 Like

I just now saw you have 2 emitters, this won’t be an issue -

All you have to do is basically use the same loop for that emitter

1 Like

You are doing the same, you are setting DB variable when the part is touched, before the touched function.
And use bool value in debounce only in local scripts!

Since whenever a player hits a part Every player have to wait 2.5 seconds to touch that part again because of db, instead if you make it a table this will no longer be an issue.

Thanks a lot to both of you guys, really appreciated i would give the solution checkmark to both of u but i cant still really appreciated its a great help.

You only can give solution mark to one of the answers.
Anyways no problem.

1 Like