As you can see, kill bricks are scripted and put around Roblox obbies or other games for a while. But I can see most of them react fast. Some are slow. The most common script used is:
local p = script.Parent p.Touched:Connect(function(part) if not part.Parent then return end local char = part.Parent local hum = char:FindFirstChildOfClass("Humanoid") --Or they use FindFirstChild haha. if hum then hum.Health = -1 end end end)
The script above uses the "Touched" event on BaseParts to react on player's interactions. But this could cause a huge amount of memory waste for the server. Even adding a debounce would cause trouble, if you use debounces. Other players could easily get through the brick at the last second without dying after somebody touched it.
The second method I've seen and which is bad is Checking local's character parts. Which is a local script and this can easily be exploited.
Are there any other methods? Advices in improving?
script.Parent.Touched:Connect(function(hit) if hit.Parent.Humanoid then hit.Parent.Humanoid.Health = 0 end end
brick.Touched:Connect(function(hit) if hit.Parent:FindFirstChildOfClass("Humanoid") then local character = hit.Parent character:BreakJoints() -- same thing as humanoid.Health = 0 end end
There is a service named CollectionService, it is really usefull and efficient for code which effects multible parts the same way. This is how you use it:
Make a server script in the killing part and put this code in it.
game:GetService("CollectionService"):AddTag(script.Parent, "Kill") --adds a tag to the part. script:Destroy() --script is no longer needed so might as well destroy it.
Then make a server script in ServerScriptService (or anywhere really) and put this code in it.
for k,v in pairs(game:GetService("CollectionService"):GetTagged("Kill")) do --loop over every part with the 'Kill' tag v.Touched:Connect(function(p) --fires when something touched the tagged part local humanoid = p.Parent:FindFirstChild("Humanoid") --find out if 'p' is a player if humanoid then humanoid.Health = 0 --kill the player end end) end
You can use Region3 for it. For a tutorial, watch this video.
https://www.youtube.com/watch?v=qOhZi1jUEvI