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

Other multiple methods or advices in making kill bricks scripts?

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?

4 answers

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

2 comments

0

Not areas, he want's "Bricks"that kill players.

0

That answer is completely incorrect. What would you expect from checking frequently Region3? Just creating waste of memory. I can see that you didn't read the post carefully.

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