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

Touched:connect(function(Part) still detecting humanoid?

Hi, so i have this:

script.Parent.Upgrader.Touched:connect(function(Part)
	wait()
	if not Part.Name == 'Egg' then return end
	wait()
	CurCashVal = Part.Cash.Value
	MathVal = CurCashVal * Boost.Value
	NewCashVal = MathVal + CurCashVal
	Part.Cash.Value = NewCashVal
	local GetTexture = Texture:Clone()
	GetTexture.Parent = Part
	GetTexture.Color3 = PackageColor
end)

However, when my players hit it, it keeps printing: Cash is not a valid member of Part "Workspace.HeadlessDeathSpeaker.Pride Flag.Handle"

Meaning that my player would be named Egg, right?

What's going wrong and how to prevent?

1 answer

You can simply detect if the part have humanoid by using :FindFirstChild()

For Example:

script.Parent.Touched:Connect(function(Hit)
    if Hit.Parent:FindFirstChild("Humanoid") then
          print("A Character Touched!")
   else
          print("A Block Touched!")
    end
end)

So in your script (Line 3) its basically wrong. Cause when you touched it, the part that touch is not you, but it was your body part (Head,Torso,etc.). So, You can change your script!

From This:

if not Part.Name == 'Egg' then return end

To This:

if not Part.Parent.Name == "Egg" then return end

Hope It Helps!

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