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:
The part would print many times. But less than the other script. (Depending on the server's response speed adding the debounce.)
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:
A repro on the devforum: https://devforum.roblox.com/t/repro-touched-events-delayed/38118
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.
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)
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.