KillBricks not working

Hello DevForum,

I’m currently in the process of making a few obstacles for my game.

I’m making one of those killbrick jump things but the killbricks are not working.

this is my code:

local KillParts = script.Parent.KillParts.KillPart

KillParts.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		hit.Parent:FindFirstChild("Humanoid").Health = 0
	end
end)
1 Like

Are there multiple KillParts that have the same name in the KillParts folder?

No you are not looping, you are basically getting the touched event of a table

:GetChildren() returns a table, not a BasePart. Instead, you would do:

local KillParts = script.Parent.KillParts:GetChildren()

for _, killPart in ipairs(KillParts) do
    if v:IsA("BasePart") then
        killPart.Touched:Connect(function(hit)
            -- script
        end)
    end
end

Yeah there are about 27 Parts.

Then I would suggest using the short piece code I wrote and adapt it to your needs.

No need to clone the script if he can do it all within one.

Woah, that is completely unnecessary and in-efficient. You can as mentioned above loop through all parts.

Not that good of scripting, but I do know this.

After the line: if hit.Parent:FindFirstChild(“Humanoid”) then

link the humanoid to another variable, then declare the “.Health” property to 0

Hope helps

But if there are 27 parts that kill, 27 scripts is inefficient

He could just put them in a folder and duplicate a preexisting one.

i haven’t tested this code, but this hypothetically should work
put all your killbricks in a model
if this works, this script could be anywhere to reference the model and get all bricks inside it to kill. not sure if it works with a folder, you could try.

local killBricks = killbrickmodel
local killBrickchildren = killBricks:GetChildren()

for i = 1, #killBrickChildren do
part = killBrickChildren[i]
part.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		hit.Parent:FindFirstChild("Humanoid").Health = 0
	end
end)
end

That doesn’t make any sense. You don’t need to have individual script for each part, as this is just redundant. If you want to do that, you just clone the main part.

Something wierd just happened.
If i die now, the camera stays where it was when i died.

What if he wanted to change how kill bricks would work? He would have to go through tons of scripts and change all of them.

Lol i just would love to spend my afternoon changing 27 identical scripts!

You would like to change a ton of scripts?

try this
I know it works for children inside a model, so i assume events like tocuched would work too. as for a folder I’m not sure.

The problem has already been solved, but thank you for your efforts!

1 Like

Let’s suggest you have a lot of kill bricks and they are in a folder, so you would do:

local KillParts = game.Workspace.KillParts:GetChildren()

for index, killPart in pairs(KillParts:GetChildren()) do
     if killPart:IsA("BasePart") then
         killPart.Touched:Connect(function(Hit)
             if Hit.Parent:IsA("Model") and Hit.Parent:FindFirstChild("Humanoid") then
              Hit.Parent:BreakJoints() --This will instantly kill the player, because it will break the joints of the character.
           end
       end)
   end
end