Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Can someone fix this kill on touch script?

Asked by 9 years ago

Please make your question title relevant to your question content. It should be a one-sentence summary in question form.

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
1
Do you have an event connection line? NotsoPenguin 705 — 9y
0
If my answer helped please Accept answer and Upvote please. woodengop 1134 — 9y
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". Redbullusa 1580 — 9y
0
^ Goulstem 8144 — 9y

2 answers

Log in to vote
1
Answered by
woodengop 1134 Moderation Voter
9 years ago

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)
0
Thx I can't believe i missed this lol LordZerefu 30 — 9y
0
This is what I thought, but not having a connection line is unrelated to the error message. NotsoPenguin 705 — 9y
0
after I've been Hacked, I've lost 2 Reps. woodengop 1134 — 9y
0
A connection line is not an end, you should probably edit your answer. Goulstem 8144 — 9y
0
sorry. woodengop 1134 — 9y
Ad
Log in to vote
1
Answered by
Goulstem 8144 Badge of Merit Moderation Voter Administrator Community Moderator
9 years ago

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

Answer this question