Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How do you make Touched event's debounce filter?

So I have a folder named "KillBricks". And many parts in there. And I created this script:

local Folder = script.Parent

for i,part in pairs(Folder:GetChildren()) do
	if part:IsA('BasePart') then
		part.Touched:connect(function(poc)
			if not poc.Parent  then return end
			if not poc.Parent:FindFirstChild('Humanoid') then return end
			local hum = poc.Parent.Humanoid
			hum.Health = 0
			print('Damaging Player')
		end)
	end
end

As you can see. Everytime the player touches the Kill Brick. It will kill the player.

But at the print() function. It will print many many times. Making the performance get worse.

But if I add a debounce to it like this:

local Folder = script.Parent

for i,part in pairs(Folder:GetChildren()) do
	if part:IsA('BasePart') then
		local debounce = false
		part.Touched:connect(function(poc)
			if not debounce then
			if not poc.Parent  then return end
			if not poc.Parent:FindFirstChild('Humanoid') then return end
				debounce = true
				local hum = poc.Parent.Humanoid
				hum.Health = 0
				print('The player has been killed')
				wait(3)
				debounce = false
			end
		end)
	end
end

Then It wouldn't work as expected:

  1. The part would print many times. But less than the other script. (Depending on the server's response speed adding the debounce.)

  2. It would give the opportunity for other players to jump on the Kill Brick without dying for 3 seconds.

So how do you make a filter for each of the Humanoid in the Touched event?

Edit: One thing I should mention that is:

  1. A repro on the devforum: https://devforum.roblox.com/t/repro-touched-events-delayed/38118

  2. If you turn it into a Local Script. Taking one of the Local character's parts and fire the server to take the request. It will work BUT if there's any exploit that deletes the LocalScript itself then the players should've been god already.

2 comments

0

Theres a topic about this at the end of the wiki: http://wiki.roblox.com/index.php?title=Debounce

0

@xEiffel Clearly you didn't read the whole question didn't you? And It even say obviously "only allow the player to be hurt by the part every 3 seconds"

2 answers

Okay I'm not positive that this will work but just attempt it with your script inside.

local Previous = {}
local Interval = 3

script.Parent.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
	if Previous[hit.Parent.Name] then return end -- if it finds the players name it removes it
	table.insert(Previous, hit.Parent.Name) -- inserting the players name into the table
-- ENTER UR SCRIPT HERE.
	wait(Interval)
	table.remove(Previous, 1) -- removing the name
-- ENTER ANYTHING AFTER IT.
	end
end)

1 comment

0

Sorry but this doesn't work. Including being fired multiple times.

local Folder = script.Parent

for i,part in pairs(Folder:GetChildren()) do
    if part:IsA('BasePart') then
        part.Touched:connect(function(poc)
            if poc.Parent:FindFirstChild('KillBrickDebounce') then return end
            if not poc.Parent  then return end
            if not poc.Parent:FindFirstChild('Humanoid') then return end
            local Debounce = Instance.new('BoolValue', poc.Parent)
            Debounce.Name = 'KillBrickDebounce'

            local hum = poc.Parent.Humanoid
            if hum.Health <= 0 then return end
            hum:TakeDamage(hum.MaxHealth) -- If this doesn't work, try hum:TakeDamage(100 or the max health every player might have)
            print('Damaging', poc.Parent.Name)
            -- Here you can add a wait(time) if you don't feel okay with this.
            Debounce:Destroy()
        end)
    end
end

I hope this helped! Note: This REALLY works, i tried it myself.

2 comments

0

No no no! This is just like the second script I posted. But this one is even worse. This creates a mass fire for the function.

0

Here we have Gordon Ramsay of scripting! let me fix my script then

This discussion is preserved as it was. New answers, comments, and votes are closed.