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

Touched and TouchEnded Function Error Help?

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

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)

1 comment

0

Yo thanks my dude!

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!

1 comment

0

Thank you very much!

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