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

How to unanchor parts defined by GetChildren?

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

✓ Accepted answer

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.

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