Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

What is the script that kill you when you touch the part?

function onTouched(part) local h = part.Parent:findFirstChild("Humanoid") if h~=nil then h.Health = 0 end end script.Parent.Touched:connect(onTouched)

function onTouched(part) local h = part.Parent:findFirstChild("Humanoid") end end script.Parent.Touched:connect(onTouched)

function onTouched(part) local h = part.Parent:findFirstChild("Humanoid") if h~=nil then h.Health = 0 end

1 comment

0

Please use code blocks when you're typing a script.

3 answers

This one is more simple, since i assume you are a beginner!

local Part = script.Parent -- Here we are creating a variable, so we can acess the part easier than needing to type script.Parent everytime we need to adjust the part

Part.Touched:Connect(function(hit) -- So here is a new thing for you. Events basically fire something when you do something!
	if hit.Parent:FindFirstChild("Humanoid") then -- Hit is the part that you touch the brick with. And if it contains a humanoid we want to fire something.
		hit.Parent.Humanoid.Health = 0 -- We are saying the health of the humanoid equals to 0. Remember to have to equal signs if it is in a if statement.
	end
end) -- Self-Explanatory.

Good luck on your scripting progress

2 comments

1

I was just about to type that script in lol, i was 7 minutes too late

0

Yep. Nice job explaining it.

This is the working one, the first one

function onTouched(part)  -- a function for when a part is touched
 local h = part.Parent:findFirstChild("Humanoid") -- find a humanoid inside the toucher
if h~=nil then -- if the humanoid is not nil then
 h.Health = 0 -- make the humanoids health 0
end 
end 
script.Parent.Touched:connect(onTouched) -- connecting it to the function

This is one way to do it.

script.Parent.Touched:Connect(function(hit)
	-- if the script.Parent is touched, the function activates
	if game.Players:GetPlayerFromCharacter(hit.Parent) then
		-- this makes so it has to be a player, no NPCs
		local hum = hit.Parent:FindFirstChild("Humanoid")
		if hum then
			-- checks to see if it has a humanoid
			hit.Parent:BreakJoints()
			-- breaks the joints in the character, killing it
		end
	end
end

This discussion is preserved as it was. New answers, comments, and votes are closed.