So you wanna make a kill brick eh?

  • What I see in most obstacle courses are kill bricks which all contain a script that handles the .Touched event which kills the player when touched. This is extremely inefficient because sometimes people create many instances without keep their code dry.

    • D on't
    • R epeat
    • Y ourself

    What you could just do is name every kill brick "Kill Brick" and then make a script in ServerScriptService which contains the following code:

    for _, killBrick in pairs(workspace:GetChildren()) do
    	if killBrick:IsA("BasePart") and killBrick.Name == "Kill Brick" then
    			local humanoid = hit.Parent:FindFirstChild("Humanoid")
    			if humanoid then
    				humanoid.Health = 0

    Boom! It took just 10 lines to make all of the kill bricks in the game function properly.

  • This only works for direct children of workspace. It doesn't work for all descendants of workspace. Also, your kill brick works for any thing that has a humanoid, which is something you normally wouldn't want.

    local function OnTouched(x)
       if game.Players:GetPlayerFromCharacter(x.Parent) then
    local function KillBricks(x)
       for _,v in next, x:GetChildren() do
          if v:IsA("BasePart") and v.Name == "Kill Brick" then
          if #v:GetChildred() > 0 then

    This function would fix this, I think.

  • @OldPalHappy Not sure if it works or not, but thanks. I was thinking if I should include the 'GetPlayerFromCharacter()' function in my tutorial.

  • @UltimateRaheem It should. I haven't tested it, but if it doesn't work it would be something very small like using x instead of v, or vice versa.

  • @OldPalHappy Sorry for bothering but.. Do you mind explaining that last part "if #v:GetChildren()" because I'm new to scripting.

    Thanks in advance.

  • Global Moderator

    @kudorey619 # is the length operator. You can use it to get the length of a
    table or string. GetChildren returns a table containing all the direct children
    of an object. Therefore, #object:GetChildren() would return the number of
    children of an object.

  • @Link150 Thank you.

Log in to reply
