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

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

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

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

6 Likes

Try using this code:

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 Player = part.Parent -- This will refer to the whole player model
local Torso = Player.Torso -- This will refer to the player'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

21 Likes

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

8 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
5 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…?

2 Likes

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.

2 Likes

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

3 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:

20 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:

4 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.

4 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)

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