roblox
Tutorial page
This article is an easy tutorial.
Scripting
This page is a tutorial about Scripting.

Hey, NuclearEntity here! You know those red killbricks in obbies? Well in this tutorial I'm going to show you how to create one!

Steps

These are all the steps to making your killbrick!

Step 1

First, insert a part and change the properties to how you want it to be.

Step 2

Insert a script inside the part you created, and open it.

Step 3

Scripting time! First type the following in:

local KillBrick = script.Parent

Step 4

Let's now add an event that will fire every time something hits the part.

KillBrick.Touched:Connect(function(hit)
    
end)

Step 5

Now we are going to code the main part of this script. First, inside the event we made, we will check if hit's parent contains a humanoid. This is important because we only want our code to kill players and NPCs. Then, we will check if the player/NPC's humanoid's health is greater than 0. We don't want our killbrick killing players/NPCs that are already dead! If the health is greater than 0, then we damage it by its MaxHealth(so it kills it).

local humanoid = hit.Parent:FindFirstChildOfClass("Humanoid")
if humanoid ~= nil then
    if humanoid.Health > 0 then
        humanoid:TakeDamage(humanoid.MaxHealth)
    end
end

Final Result

If you coded it correctly, it should look like this:

local KillBrick = script.Parent

KillBrick.Touched:Connect(function(hit)
	local humanoid = hit.Parent:FindFirstChildOfClass("Humanoid")
    if humanoid ~= nil then
        if humanoid.Health > 0 then
            humanoid:TakeDamage(humanoid.MaxHealth)
        end
    end
end)

Conclusion

Hope it helps! If you have any errors, comment it. ~NuclearEntity