Debounce Assistance

Hey everyone,

As the title states I’m currently trying to fix an issue in my game that involves debounce. I’ve made a fire spell that sends 1-5 bricks with fire particles out when it hits a target but the problem is that instead of spawning all the bricks at once it drip feeds them in like this

This is the debounce script I drafted up which is albeit a bit rough around the edges.

local debounce = false 
function OnTouched(Part) 
if Part.Parent ~= nil then
if debounce == false and Part.Parent:findFirstChild("Monster") ~= nil then 
debounce = true 
			for i =1,5 do
			local human = Part.Parent:findFirstChild("Monster")
			local Splash = Instance.new("Part")
			Splash.Parent = game.Workspace
			Splash.Position = human.Torso.Position + Vector3.new(math.random(-5,5),math.random(-5,5),math.random(-5,5))
			Splash.Shape = 1
			Splash.formFactor = "Plate"
			Splash.Name = "Splash"
			Splash.BrickColor = BrickColor.new(26)
			Splash.Transparency = 1
			Splash.Size = Vector3.new(1,0.8,1)
			Splash.Velocity = Vector3.new(math.random(-70, 70),math.random(-70, 70),math.random(-70, 70))
			local Aura = script.Smoke:clone()
			Aura.Parent = Splash

			game:GetService("Debris"):AddItem(Splash, 3) 
wait(0.3)
debounce = false 
end 
end 
	end 
	end
script.Parent.Touched:connect(OnTouched)

This is a clip of how I want it to behave but with less particles, hence the debounce since it appears to be hitting multiple times.

I’ve also posted the script without my attempts at adding debounce below.

local Pool = script.Parent

function onTouched(hit)
	print("Pool hit")

	local human = hit.Parent:findFirstChild("Monster")

	if (human ~= nil ) then 
		for i =1,5 do
			local Splash = Instance.new("Part")
			Splash.Parent = game.Workspace
			Splash.Position = human.Torso.Position + Vector3.new(math.random(-5,5),math.random(-5,5),math.random(-5,5))
			Splash.Shape = 1
			Splash.formFactor = "Plate"
			Splash.Name = "Splash"
			Splash.BrickColor = BrickColor.new(26)
			Splash.Transparency = 1
			Splash.Size = Vector3.new(1,0.8,1)
			Splash.Velocity = Vector3.new(math.random(-70, 70),math.random(-70, 70),math.random(-70, 70))
			local Aura = script.Smoke:clone()
			Aura.Parent = Splash

			game:GetService("Debris"):AddItem(Splash, 3)
			
						
		end
	end
	
end


script.Parent.Touched:connect(onTouched)

Any help would be appreciated, thanks!

Does this work?

local Pool = script.Parent
local debounce = false
function onTouched(hit)
	print("Pool hit")

	local human = hit.Parent:FindFirstChild("Monster")

	if (human ~= nil ) then 
		if not debounce then
			debounce = true

			for i =1,5 do
				local Splash = Instance.new("Part")
				Splash.Parent = game.Workspace
				Splash.Position = human.Torso.Position + Vector3.new(math.random(-5,5),math.random(-5,5),math.random(-5,5))
				Splash.Shape = 1
				Splash.formFactor = "Plate"
				Splash.Name = "Splash"
				Splash.BrickColor = BrickColor.new(26)
				Splash.Transparency = 1
				Splash.Size = Vector3.new(1,0.8,1)
				Splash.Velocity = Vector3.new(math.random(-70, 70),math.random(-70, 70),math.random(-70, 70))
				local Aura = script.Smoke:clone()
				Aura.Parent = Splash

				game:GetService("Debris"):AddItem(Splash, 3)

			end
		end
		wait(0.03)
		debounce = false
	end

end


script.Parent.Touched:Connect(onTouched)

Ofcourse, I don’t understand the behavior or object classes of anything in your game because I’m merely just helping you understand the code more so here’s a rough draft of something that may be better optimized.

local debounce = false
script.Parent.Touched:Connect(function(hit)
	if debounce ~= true then
		local success, response = pcall(function() -- Incase this scenario fails, let's make it a pcall so debounce can become false again.
			local character = hit.Parent
			local humanoid = character:FindFIrstChildWhichIsA("Humanoid") -- if this is the true "monster" object i'd check the name
			if typeof(humanoid) == 'Instance' and humanoid:IsA("Humanoid") and character.Name == 'Monster' then -- validate humanoid (maybe Monster too)
				for i = 1, 5 do
					local Splash = Instance.new("Part")
					Splash.Parent = game.Workspace
					Splash.Position = character.Torso.Position + Vector3.new(math.random(-5,5),math.random(-5,5),math.random(-5,5))
					Splash.Shape = 1
					Splash.formFactor = "Plate"
					Splash.Name = "Splash"
					Splash.BrickColor = BrickColor.new(26)
					Splash.Transparency = 1
					Splash.Size = Vector3.new(1,0.8,1)
					Splash.Velocity = Vector3.new(math.random(-70, 70),math.random(-70, 70),math.random(-70, 70))
					local Aura = script.Smoke:Clone()
					Aura.Parent = Splash
					game:GetService("Debris"):AddItem(Splash, 3) 
					wait(0.3)
				end 
			end
		end)
		if success ~= true then
			warn(response)
		end
		debounce = false 
	end
end)

Wrap the creation of the splash parts in a function and call the function 3 times before activating the debounce.

Usually when creating games, I like to create a debounce table for all of the players inside of the game.

Here is an example:

local Players = game:GetService("Players")
local Debounce = {}

Players.PlayerAdded:Connect(function(player)
    Debounce[player.Name] = false
end)

Players.PlayerRemoving:Connect(function(player)
    Debounce[player.Name] = nil
end)

Instead of storing your script inside of the tool, you could fire to the server to do these actions and use this sort of debounce system.

And here is a fixed version of your code:

local debounce = false

local addDebounce = function(debounce)
    coroutine.resume(coroutine.create(function()
        debounce = true
        task.wait(debounce)
        debounce = false
    end))
end

function OnTouched(Part)
    if Part.Parent ~= nil then
        if debounce == false and Part.Parent:FindFirstChild("Monster") ~= nil then
            for i = 1, 5 do
                local human = Part.Parent:FindFirstChild("Monster")
                local Splash = Instance.new("Part")
                Splash.Parent = game.Workspace
                Splash.Position = human.Torso.Position + Vector3.new(math.random(-5, 5), math.random(-5, 5), math.random(-5, 5))
                Splash.Shape = 1
                Splash.formFactor = "Plate"
                Splash.Name = "Splash"
                Splash.BrickColor = BrickColor.new(26)
                Splash.Transparency = 1
                Splash.Size = Vector3.new(1, 0.8, 1)
                Splash.Velocity = Vector3.new(math.random(-70, 70), math.random(-70, 70), math.random(-70, 70))
                
                local Aura = script.Smoke:Clone()
                Aura.Parent = Splash
                game:GetService("Debris"):AddItem(Splash, 3)
                
                addDebounce(0.3)
            end
        end
    end
end

script.Parent.Touched:Connect(OnTouched)

Give this a try and let me know, also beautified the code a little bit and made it nicer :smiley:

It sure does! And now I see where I went wrong, thanks!

1 Like