Instantly Kill on Brick Touch

I am trying to create an obstacle course, so I need a brick that instantly kills the player when it’s touched. In the example below the red brick is supposed to kill instantly, but if you hold jump you can avoid the kill. Does anybody know any better ways to do this?

The Script
function onTouched(h)
	local h = h.Parent:FindFirstChild("Humanoid")
	if h ~= nil then
			h.Health = 0
	end
end

script.Parent.Touched:Connect(onTouched)
The Error

If you hold jump you can avoid being killed;
https://gyazo.com/0a2990ce067e571bc652e6919a8c793e

50 Likes
function onTouched(Obj)
	local h = Obj.Parent:FindFirstChild("Humanoid")
	if h then
		h.Health = 0
	end
end

script.Parent.Touched:Connect(onTouched)
59 Likes

This does not fix the error I am having; it’s virtually the same code I wrote

8 Likes

Try using this code:

local lava = script.Parent -- script.Parent will refer to the Part
	 
function kill(part) -- You use the keyword "function" to declare a function and the word after it is the name of the function and the word between the brackets is called the parameter, in this example it will refer to the object that touched the Lava
	 
  local character = part.Parent -- This will refer to the whole character
  local torso = character.Torso -- This will refer to the character's torso
	 
  torso:Destroy() --This will kill the player
	 
end --This is used to signify the end of the function
	 
lava.Touched:Connect(kill) -- We use the .Touched event to call our function

17 Likes

I would add a short jump cooldown of like 1/2 a second and that should fix the error

7 Likes

How would this refer to the player? I am getting an error in output, here it is;

Output Error

21:37:39.311 - Torso is not a valid member of Model

21:37:39.312 - Stack Begin

21:37:39.312 - Script 'Workspace.Cube.KillScript', Line 6

21:37:39.313 - Stack End
4 Likes

Try setting the kill brick to can-collide false.

24 Likes

I’ve tested it, it does kill the player. So, not sure why that is doing that. As I don’t get that error when I use it.

1 Like

Where is your brick located? In the workspace?

Is your script a local script, modular script…?

1 Like

Yes, it’s a script and it’s in the workspace, make sure the script’s parent is the block that you want to kill the player with.

1 Like

This code may not work with r15, since the torso is divided into UpperTorso and LowerTorso.

2 Likes

Yep, I think that’s the answer. Could HumanoidRootPart work?

2 Likes

I would just go with head. However, I’m not sure if this will fix the problem from the OP. The OP is trying to find a more reliable way to use .Touched events, since they sometimes don’t fire.

2 Likes

I assume your kill brick works if you don’t hold the jump button?
Instead of making the red part be the kill brick, you should instead make a non-collidable invisible part offset by one or 1.5 studs up. That way, you don’t only target the jumping people, but also the people wearing the levitation pack :slightly_smiling_face:

22 Likes

You can still spam jump to avoid the kill

1 Like

You can make an invisible not collisionable part on top of the red part, as it has no collisions, the character will sink into the part. Edit: @PlantChampion we wrote the same at the same time.

3 Likes

and @Nightrains (I won’t forget you) this works as a temporary solution . I hope in the future there could be a better solution, but thanks for bringing this idea to my attention! :smile:

3 Likes

To fix this in my game I made all the events and tweens on the clients. Since it’s on the client there is no latency and things happen immediately as well as lagless tweens. The events are on the client though, which means it can be easily exploited but it works pretty well.

3 Likes

Hm… that’s actually pretty smart, I may try this. Exploitations are not too big of an issue I am looking out for so it’s possibly something I could look into.

Love the videos, by the way. I learned a lot from your trading guides (even though I got scammed)

5 Likes
script.Parent.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		hit.Parent:BreakJoints()
	end
end)
36 Likes