Today I’m gonna show you how to make a kill brick for your Roblox game. Let’s get started
To create a Kill Brick in Roblox using Luau I have created a script to help you with this task.
` local part = script.Parent
local function onTouch(otherPart)
local character = otherPart.Parent
local humanoid = character:FindFirstChild(“Humanoid”)
if humanoid then
humanoid.Health = 0 -- Instantly kills the player
-- Alternative: humanoid:TakeDamage(100) -- Does 100 damage
end
end
part.Touched:Connect(onTouch) `
Insert a part into your game (this will be your kill brick)
Insert a Script into the part
Paste the code above into the script
Variations:
With respawn effect:
` local part = script.Parent
local function onTouch(otherPart)
local humanoid = otherPart.Parent:FindFirstChild(“Humanoid”)
if humanoid then
humanoid.Health = 0
– Optional visual effects:
part.BrickColor = BrickColor.new(“Really red”)
local explosion = Instance.new(“Explosion”)
explosion.Position = part.Position
explosion.Parent = workspace
end
end
part.Touched:Connect(onTouch) ```
I hoped you enjoy this tutorial have a great day and a great kill brick!
I mean this is nice for beginners, but how would they learn? They don’t understand the material they are using? Add a description for what each line does, and please format your post with proper code blocks using the ` 3 times usually.
This script is only well optimized for a single kill brick, for multiple kill bricks - which you will probably be using - use the CollectionService with tags. You only need one script with this approach. Also, adding a debounce will allow you to take damage slower. I also added some comments to help show what each part does.
local CollectionService = game:GetService("CollectionService")
local TAG = "KillBrick" -- A unique tag
local KILL_BRICK_DAMAGE = 25 -- Damage per second
-- Listen for .Touched on a new killBrick
local function NewKillBrick(killBrick)
local debounce = false -- Prevents player from touching part constantly
killBrick.Touched:Connect(function(touchedPart: BasePart) -- Part was touched
local character = touchedPart.Parent
local humanoid = character:FindFirstChild("Humanoid")
if humanoid then
if debounce then return end -- Don't continue if debounce == true
debounce = true -- Don't allow player to touch part for now
humanoid:TakeDamage(KILL_BRICK_DAMAGE)
task.wait(1) -- Wait before allowing player to touch part
debounce = false
end
end)
end
local initiallyTagged = CollectionService:GetTagged(TAG) -- Get kill bricks
for _, killBrick in initiallyTagged do -- Loop through kill bricks
NewKillBrick(killBrick)
end
CollectionService:GetInstanceAddedSignal(TAG):Connect(function(killBrick) -- New kill brick added
NewKillBrick(killBrick)
end)
For this method:
Create a script with this code; it’s probably going to have errors so have fun debugging (it’s good experience)
Create a kill brick and add the tag specified in the script (here it’s “KillBrick”)
-- Place this script inside the Kill Brick part
local killBrick = script.Parent
killBrick.Touched:Connect(function(hit)
local character = hit.Parent
local humanoid = character:FindFirstChild("Humanoid")
if humanoid then
humanoid.Health = 0 -- Kills the player
end
end)
That’s the point of tutorial it teaches you how to use the script. You can learn the script and learn how it works but this tutorial isn’t a lecture or meeting seminar.