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

Touched and TouchEnded Function Error Help?

Asked by
lysandr 49
6 years ago
Any suggestions as to how to prevent this error I've been getting from this script I made.

script.parent.houseSize.TouchEnded:connect(function(touch) if touch.Parent.Humanoid then print("idk") end end)

the error:

20:51:57.846 - Humanoid is not a valid member of Accessory 20:51:57.847 - Stack Begin 20:51:57.847 - Script 'Workspace.miscScripts.Script', Line 26 20:51:57.848 - Stack End

2 answers

Log in to vote
0
Answered by
Goulstem 8144 Badge of Merit Moderation Voter Administrator Community Moderator
6 years ago
Edited 6 years ago

You're close :)

You have to check it with the FindFirstChild function, otherwise you could be indexing a nil value.

script.parent.houseSize.TouchEnded:connect(function(touch) 
    if touch.Parent:FindFirstChild("Humanoid") then --FindFirstChild function
        print("idk") 
    end 
end)
0
Yo thanks my dude! lysandr 49 — 6y
Ad
Log in to vote
0
Answered by 6 years ago

Hi!

It seems like one of your hats is coming in contact with the houseSize, cuing the function. The problem with hats is that their parent is not the character, but instead its parent's parent.

Like this: Character > Accessory > Handle

Also, instead of using "Parent.Humanoid", you need to use "Parent:FindFirstChild("Humanoid")" if you're trying to determine if there is a humanoid within the model. Otherwise, if there is no humanoid it will cause a nil error.

script.parent.houseSize.TouchEnded:connect(function(touch)
    if touch.Parent:FindFirstChild("Humanoid") or touch.Parent.Parent:FindFirstChild("Humanoid") then
        print("idk")
    end
end)

Click Accept Answer if this was what you were looking for! Comment if you have any more concerns!

0
Thank you very much! lysandr 49 — 6y

Answer this question