Do you have any solution on how to make a kill brick script?

Hello! I’m messing around the kill brick script that i made but somehow the output has no sign of error and the script isn’t working is there any solution, also please kindly explain to me the issue I am facing so I can get a better understanding of it and learn from my mistake.

script.Parent.Touched:Connect(function(OtherPart)
local player = game.Players:GetPlayerFromCharacter(OtherPart)
if player then
player.Character.Humaniod.Health = 0
end
end)

I’m standing on the Brick nothing happens :confused:

10 Likes

Could you at least provide the code in a codeblock? I can’t really be bothered to watch the whole video just to debug someone’s code

1 Like

There is kill bricks in the toolbox you can get it there. You could just look at the code of the kill brick.

Just search kill

3 Likes

script.Parent.Touched:Connect(function(OtherPart)
local player = game.Players:GetPlayerFromCharacter(OtherPart)
if player then
player.Character.Humaniod.Health = 0
end
end)

script.Parent.Touched:Connect(function(OtherPart)
	local Humanoid = OtherPart.Parent:FindFirstChild("Humanoid")
	
	if Humanoid then
		Humanoid.Health = 0
	end
end)

:GetPlayerFromCharacter() never really works for .Touched events

2 Likes

That is also a solution, but he wants to learn from his mistakes

:GetPlayerFromCharacter() works perfectly fine with Touched events. He should be getting the player through

--...
local plr = game.Players:GetPlayerFromCharacter(OtherPart.Parent)
if plr then
--...

There’s a good chance that if it didn’t work for you, there was definitely something wrong with your code.

1 Like

You spelled Humanoid wrong in your example, that might be a part of the problem.

Everytime I used :GetPlayerFromCharacter() on a .Touched event, it never worked, I’ll experiment with it later if I’ll have some time

How I would usually write a kill script is

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

To reduce the amount of method calls, you should probably check for Humanoid and then check if it’s Player instance.

script.Parent.Touched:Connect(function(OtherPart)
	local Humanoid = OtherPart.Parent:FindFirstChild("Humanoid")
	if Humanoid then
       local Player = game:GetService("Players"):GetPlayerFromCharacter(OtherPart.Parent)
       if Player then -- This will only kill Players. Not NPCs.
	   	  Humanoid.Health = 0
       end
	end
end)
1 Like

I like using this:

local function onTouched(hit)
local model = hit:FindFirstAncestoryWhichIsA("Model")
local humanoid = model:FindFirstChildWhichIsA("Humanoid")
if humanoid then
model:BreakJoints()
end
end

the simplest way I know to make a killbrick script is this:

script.Parent.Touched:Connect(function(hit)
local Humanoid = hit.Parent:FindFirstChild("Humanoid")
if Humanoid then
Humanoid.Health = 0
end
end)
--note that if you had an NPC in your game, this would kill it 
--as well if touched, so if you have a setup like that, you should
--add a check for players in between.

Try out this script:

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

Hope it helps :slight_smile:

What I would recommend for kill bricks, is using Collection Service and Tagging all the Kill bricks as something like Kill Bricks and then in A Server Script, you loop through all the objects tagged as Kill Bricks and basically use something like this script

for _, killBrick in pairs(CollectionService:GetTagged("Kill Bricks")) do
    killBrick.Touched:Connect(function(hit)
        local player = game.Players:GetPlayerFromCharacter(hit.Parent) --Basically check if its a player or not.

        if player then 
             player.Character.Humanoid.Health = 0
        end
    end)
end

I use Collection Service for two main reasons:

  1. Its easier, because you don’t need to keep pasting scripts for new kill bricks, just tag it and done.
  2. If you have any bugs, in the code you would have to edit each and every script, but if you use Collection Service, you just need to change that one script.
3 Likes

This script isnt working for me, all of my parts are tagged as KILL, and the part name for them is killer

1 Like

I think I have an idea:

function onTouched(Obj)
local h = Obj.Parent:FindFirstChild(“Humanoid”)
if h then
h.Health = 0
end
end

script.Parent.Touched:Connect(onTouched)

2 Likes

You tagged them with Collection editor? If not then can you tell me how are you tagging them? Also I just gave that as an example, didn’t mean to spoon feed you.

Just a suggestion anyways if you find a script in someone’s solution try to debug it if something isn’t working, because it seems to be working for me.

I combined it and a personal script, and edited a bit, used the tag editor plugin to tag the parts

Well in your part create a script and enter that:
script.Parent.Touched:Connect(function(hited)
if hited and hited.Parent and hited.Parent:FindFirstChild(“Humanoid”) then
hited.Parent.Humanoid.Health = 0
end
end)