Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How do you make Touched event's debounce filter?

Asked by 6 years ago
Edited 6 years ago

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.

0
Theres a topic about this at the end of the wiki: http://wiki.roblox.com/index.php?title=Debounce xEiffel 280 — 6y
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" magicguy78942 238 — 6y

2 answers

Log in to vote
0
Answered by
xEiffel 280 Moderation Voter
6 years ago

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)
0
Sorry but this doesn't work. Including being fired multiple times. magicguy78942 238 — 6y
Ad
Log in to vote
0
Answered by 6 years ago
Edited 6 years ago
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.

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. magicguy78942 238 — 6y
0
Here we have Gordon Ramsay of scripting! let me fix my script then SuperAndresZ_YT 202 — 6y

Answer this question