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

Why inst this working?

Asked by
duckyo01 120
8 years ago

I'm just trying to make a single lava brick and this script won't work for some reason.

function onTouch(part)
    local Guy = script.Parent:findFirstChild("Humanoid")
    if (Guy == nil) then return end
    Guy.Health = 0

end
script.Parent.Touched:connect(onTouch)

2 answers

Log in to vote
2
Answered by 8 years ago

You're checking the script's parent for the humanoid, instead of the "part". part in this case is what touched the script's parent, so that's the object we would search the humanoid for.

Try this:

function onTouch(part)
    -- checking "part.Parent" for humanoid
    local Guy = part.Parent:findFirstChild("Humanoid")
    if (Guy == nil) then return end
    Guy.Health = 0
end

script.Parent.Touched:connect(onTouch)

We could even clean this up a bit, not that it would make much of a difference:

-- Create the function in the process of declaring the event.
script.Parent.Touched:connect(function(Part)
    local Human = Part.Parent:FindFirstChild'Humanoid'
    if Human then -- This will return true if humanoid was found.
        Human.Health = 0
    end
end)

However, this is just how I'd write it. You don't need to worry to much about small details, but for the sake of readability, I'd prefer something like this.

Ad
Log in to vote
-1
Answered by 8 years ago
function onTouch(part)
    local Guy = script.Parent:findFirstChild("Humanoid")
    if (Guy ~= nil) then return end
    Guy.Health = 0

end
script.Parent.Touched:connect(onTouch)

you used == nil which would mean it doesnt exist but ~= means it does.

0
It still doesn't work duckyo01 120 — 8y
0
The problem is that you need to specify part.Parent.Humanoid rather than script.Parent.Humanoid. The == nil part is correct. funyun 958 — 8y
0
shouldn't it be "part.Parent:findFirstChild("Humanoid")" Senor_Chung 210 — 8y

Answer this question