[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:
(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”
Now, Add your killbricks (or killbricks) inside the folder. Do NOT add a script inside of them.
Now, we need to get out of the workspace, and enter the wonderful world of…
(drum roll plz)
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.
Once you finish, Delete the “Hello World”
This doesn’t even deserve a screenshot
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.
Now, Scroll down on its properties menu until you see this:
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.
Look! Your part is now tagged!
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