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

How to unanchor parts defined by GetChildren?

Asked by 5 years ago

i have this script here

local tree = workspace.Foliage:FindFirstChild("MapleTree2"):GetChildren()

tree.Touched:Connect(function(hit)
    local char = hit.Parent
    local humanoid = char:FindFirstChild("Humanoid")
    if humanoid then
        tree.Anchored = false
    end
end)

But for some reason it keeps detecting Touched as a property (from what i understand) I get this error:

18:04:34.397 - ServerScriptService.Script:3: attempt to index field 'Touched' (a nil value) 18:04:34.398 - Stack Begin 18:04:34.398 - Script 'ServerScriptService.Script', Line 3 18:04:34.399 - Stack End

1 answer

Log in to vote
2
Answered by
RAYAN1565 691 Moderation Voter
5 years ago
Edited 5 years ago

Try this:

local tree = workspace.Foliage:FindFirstChild("MapleTree2"):GetChildren()

for _, v in pairs(tree) do
    v.Touched:Connect(function(hit)
        local char = hit.Parent
        local humanoid = char:FindFirstChild("Humanoid")
        if humanoid then
            for _, part in pairs(tree) do
                part.Anchored = false
            end
     end
    end)
end

You're indexing tree as multiple objects. You cannot do a Touched event on that. You need to take each object separately.

Ad

Answer this question