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

Touched is not a valid member of attachment?

I don't know what is wrong with this script but the output says touched is not a valid member of attachment

local plr = game.Players.LocalPlayer
touched = false

repeat wait()until plr.Character.LowerTorso.Anchored == true
repeat wait()until plr.Character.LowerTorso.Anchored == false
wait(5)

for _, descendant in pairs(plr.Character:GetDescendants())do
	descendant.Touched:Connect(function(hit)
		if hit:IsA("Part") then
			touched = true
			if touched == true then
				script.Parent.MidAirAnim.Disabled = true
				script.Parent.MidAirMovement.Disabled = true
				plr.Character.Animation:Destroy()
				local CrashLandAnim = Instance.new("Animation", plr.Character)
				CrashLandAnim.AnimationId = "http://www.roblox.com/asset/?id=2296730026"
				local humanoid = plr.Character:WaitForChild("Humanoid")
				local playanim = humanoid:LoadAnimation(CrashLandAnim)
				CrashLandAnim:Play()
			end
		end
	end)
end

2 answers

That is because the player has other stuff than his body parts(e.g scripts) so what you can do is add a check to make sure that the descendant its getting in the in pairs loop is actually a body part.

local plr = game.Players.LocalPlayer
touched = false

repeat wait()until plr.Character.LowerTorso.Anchored == true
repeat wait()until plr.Character.LowerTorso.Anchored == false
wait(5)

for _, descendant in pairs(plr.Character:GetDescendants())do
if descendant:IsA("Part") then
    descendant.Touched:Connect(function(hit)
        if hit:IsA("Part") then
            touched = true
            if touched == true then
                script.Parent.MidAirAnim.Disabled = true
                script.Parent.MidAirMovement.Disabled = true
                plr.Character.Animation:Destroy()
                local CrashLandAnim = Instance.new("Animation", plr.Character)
                CrashLandAnim.AnimationId = "http://www.roblox.com/asset/?id=2296730026"
                local humanoid = plr.Character:WaitForChild("Humanoid")
                local playanim = humanoid:LoadAnimation(CrashLandAnim)
                CrashLandAnim:Play()
            end
        end
    end)
end
end

There's no ".Touched" event for attachments, and when you loop through the descendant of the character you are going through attachments, which is causing the error. So before you use the "descendant.Touched" part of the script check to make sure the descendant is not an attachment.

local plr = game.Players.LocalPlayer
touched = false

repeat wait()until plr.Character.LowerTorso.Anchored == true
repeat wait()until plr.Character.LowerTorso.Anchored == false
wait(5)

for _, descendant in pairs(plr.Character:GetDescendants())do
	if descendant.ClassName ~= "Attachment"  then
		descendant.Touched:Connect(function(hit)
       		if hit:IsA("Part") then
           		touched = true
           		if touched == true then
           			script.Parent.MidAirAnim.Disabled = true
          	     	script.Parent.MidAirMovement.Disabled = true
          	     	plr.Character.Animation:Destroy()
       	        	local CrashLandAnim = Instance.new("Animation", plr.Character)
          	     	CrashLandAnim.AnimationId = "http://www.roblox.com/asset/?id=2296730026"
           	    	local humanoid = plr.Character:WaitForChild("Humanoid")
            	   	local playanim = humanoid:LoadAnimation(CrashLandAnim)
             	  	CrashLandAnim:Play()
       	    	end
        	end
   		end)
	end
end

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