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

Why does my script error out on line 4?

Asked by
Prioxis 673 Moderation Voter
9 years ago

I have my script to where if the object touching the part is a hat then end the script else if it has a humanoid then teleport it into the house

here's my house

script.Parent.Touched:connect(function(hit)
    if hit.Parent.Name == "Hat" then
    print'nope'
    elseif hit.Parent.Humanoid then
local h = game.Players:findFirstChild(hit.Parent.Name)
h.PlayerGui.Enter.Frame.Visible = true
h.PlayerGui.Enter.Button.Visible = true
else
    end
end)

1 answer

Log in to vote
1
Answered by
Defaultio 160
9 years ago

It looks like you want to see if there is an instance called "Humanoid" as a child of hit.Parent. You want your condition hit.Parent.Humanoid to return nil if there is no humanoid there, but it's not actually safe to do this and an error will be thrown if there is no humanoid. Rather, hit.Parent:FindFirstChild("Humanoid") will do what you want, returning the Humanoid if it can be found, and nil otherwise.

0
Oh hey defaultio and thanks Prioxis 673 — 9y
Ad

Answer this question