The CORRECT way to make killbricks in studio! [FIXED]

[This tutorial has been fixed for further optimization]

Hey there! If you’re reading this tutorial, there’s a good chance you have something like this for your killbrick setup:


And it works!

However…

Just because it works doesn’t mean it’s effective. What if you were to duplicate those killbricks 1000 times in your rainbow colorful obby game?


And look at this incredibly beatiful explorer!

Well in this tutorial, that explorer will go from

To this:
Screenshot 2024-08-11 at 7.13.18 PM
(No more scripts and clutter which makes it better)

Now, there’s actually 2 ways to do this:

Way 1: Folders

First, create a folder inside of workspace, and name it “KillBricks”
Screenshot 2024-08-11 at 7.14.17 PM
Now, Add your killbricks (or killbricks) inside the folder. Do NOT add a script inside of them.
Screenshot 2024-08-11 at 7.16.21 PM

Now, we need to get out of the workspace, and enter the wonderful world of…
(drum roll plz)
Screenshot 2024-08-11 at 7.17.29 PM

Alright, let’s continue
After that, Insert a script inside of ServerScriptService
You can name it anything, preferably “KillBrickHandler”
Make sure it’s a script, not a local or module script. That will mess up everything.
Screenshot 2024-08-11 at 7.18.53 PM
Once you finish, Delete the “Hello World”
This doesn’t even deserve a screenshot :skull:
Upon getting rid of the print statement, let’s define some variables. Right now, we will define the killBricks.

local killBricksFolder = game.Workspace.KillBricks
local killBricks = killBricksFolder:GetChildren()

The GetChildren() operation will make the variable equal to the items parented under killBricksFolder. For example, if we had partA parented under PartB, if we did

local part = partB:GetChildren()

then part would be equal to partA!

After that, we need to loop through the killBricks. To do that, we can use a for loop.

for number, killBrick in killBricks do
	-- Code can go in here --
end

What this will do is it will loop through all of the items inside of killBricksFolder, defined as killBricks. This means it will do the first killbrick, then a different killbrick, you get the point, right? The first value, number, is the number of the item we’re looping through (in order), which means the first item we loop through will have a number value of 1. The second value, killBrick, is the item we are currently looping through.
Now, we need to make an anonymous touched event.

for number, killBrick in killBricks do
	killBrick.Touched:Connect(function(hit)
		-- Stuff that kills the player go here --
	end)
end

When the killbrick that is being looped through is touched, it will fire the code inside the touched event. Since we are looping through multiple killbricks, it allows to control multiple killBricks with 1 script, which is very very very very effective. Now, we insert the basic stuff to kill the player. Hopefully you know this. If you don’t then uhhhhhhh

Final Code

local killBricksFolder = game.Workspace.KillBricks
local killBricks = killBricksFolder:GetChildren()

for number, killBrick in killBricks do
	killBrick.Touched:Connect(function(hit)
		local humanoid = hit.Parent:FindFirstChild("Humanoid")
		if humanoid then
			humanoid.Health = 0
		end
	end)
end

Test your game and it should work!

Now, here’s the second method.

Using Tags

Tags are very underrated, and can be used for many purposes that many people do not know. Tags are the second way to make this advanced killBrick system.

First, insert a part into your workspace, name it anything.
Screenshot 2024-08-11 at 7.30.04 PM
Now, Scroll down on its properties menu until you see this:
Screenshot 2024-08-11 at 7.30.42 PM
This is used for adding tags. Click the plus icon, and you should see a menu pop up on your screen. Type in “KillBrick” and hit enter.
Screenshot 2024-08-11 at 7.31.34 PM
Look! Your part is now tagged!
Screenshot 2024-08-11 at 7.31.51 PM

Now, let’s start scripting.
(scary boss battle music)

First, add a script into ServerScriptService.
Rename it however you want, preferably “KillBrickHandler”.
Make sure it’s a SCRIPT, not a LOCAL SCRIPT or MODULE SCRIPT.
While you’re at it, get rid of that print statement.


First, we want to define CollectionService. Then, we want to loop through CollectionService:GetTagged() to loop through all the parts that have the tag (“KillBrick”)

local collectionService = game:GetService("CollectionService")
for number, killBrick in collectionService:GetTagged("KillBrick") do
	-- Stuff go here!!! -- 
end

Incase you didn’t know, the first value, number, is the number of the item we’re looping through (in order), which means the first item we loop through will have a number value of 1. The second value, killBrick, is the item we are currently looping through.

From here, insert an anonymous touched event.

for number, killBrick in collectionService:GetTagged("KillBrick") do
	killBrick.Touched:Connect(function(hit)
		-- Kill player --
	end)
end

Finally, insert some basic killBrick code.

Final Code

local collectionService = game:GetService("CollectionService")
for number, killBrick in collectionService:GetTagged("KillBrick") do
	killBrick.Touched:Connect(function(hit)
		local humanoid = hit.Parent:FindFirstChild("Humanoid")
		if humanoid then
			humanoid.Health = 0
		end
	end)
end

Test your game and it should work!

Conclusion

And that’s it! I hope this tutorial helped you better optimize and make killbricks in Roblox Studio. Please share this with anyone you know that would benefit from this tutorial. Also, this tutorial took a long time to make (30 minutes or so), so I would appreciate support. Thanks, and have a wonderful day, devs!

oh my god im so tired my fingers are crying out in pain

Did this help?

  • yes
  • no
  • maybe

0 voters

7 Likes

Please use CollectionService:GetTagged("KillBrick") in the tags method instead of iterating through literally every descendant. It returns array of parts that have this tag.
Otherwise there isn’t much problem to begin with this tutorial

14 Likes

Using CollectionService instead of getting descendants is better in performence i think.

@vvv331 and @computerph1_DEV the tutorial is fixed! Thanks for the feedback.

2 Likes

PS Make sure to add the GetInstanceAddedSignal and GetInstanceRemovedSignal as Parts maybe delayed to render after the iteration loop.

1 Like

I like this, not often I see tutorials aimed at the new guy

I don’t usually use Touch events because I’ve heard and experienced some issues with them but does using the Humanoid.Touched event work as well? (while checking if the touched part has the name “KillBrick” or checking if it’s a child of a certain killbrick folder?)

Humanoid.Touched | Documentation - Roblox Creator Hub

check this topic

1 Like

this tutorial is indeed useful, but can decrease performance by a ton.