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

Can someone fix this kill on touch script?

This script isn't working, it says attempt to call a nil value but not killing.


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

4 comments

1

Do you have an event connection line?

0

If my answer helped please Accept answer and Upvote please.

1

A connection line has nothing to do with the error. If the connection line's wrong, then the function itself will not run. On line 3, "~=nil" is redundant. Mind as well put it as "if h then".

0

^

2 answers

You are getting there but I can see your Mistake, There is no (connection line) to it, this is what I mean:

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

script.Parent.Touched:connect(onTouched)

5 comments

0

Thx I can't believe i missed this lol

0

This is what I thought, but not having a connection line is unrelated to the error message.

0

after I've been Hacked, I've lost 2 Reps.

0

A connection line is not an end, you should probably edit your answer.

0

sorry.

Your problem is that you never connected the function to an event.

To do so then you need to write something called a 'connection line'. Connection lines can either be in the definition of the function or outside. When they're inside, then it's called an Anonymous function

Connection line example; --NOTE: This would only work if the thing you want to touch is the script's parent

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

--[[ This is the connection statement, you tie the wanted function into the wanted event by placing the function name inside of the parenthasi]]

script.Parent.Touched:connect(onTouched)

Anonymous function example; --NOTE: This would only work if the thing you want to touch is the script's parent

--[[With anonymous functions, you define the function INSIDE the connection statement, and you don't name the function]]

script.Parent.Touched:connect(function(part)
	local h = part.Parent:FindFirstChild('Humanoid')
	if h then
		h.Health = 0
	end
end) --Remember to place the parenthasi there to end the connection

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