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

Touched is not a valid member of attachment?

Asked by 5 years ago
Edited 5 years ago

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

Log in to vote
0
Answered by
aazkao 787 Moderation Voter
5 years ago

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
Ad
Log in to vote
-1
Answered by 5 years ago

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

Answer this question