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

I cant make the Model's children unancored. Is there something wrong with my for i,v in pairs loop?

I'm trying to make the tree's leaves unancored when it chopped down. but leaves stays where it is. Is there something wrong with my for i,v in pairs loop?

Note: Wood is the part that i clicked. Tree Model structure: https://imgur.com/5yxO95I

	if Cursor.Target ~= nil and Cursor.Target.Parent.Name == 'Tree' and  Gather == true then
		local Wood = Cursor.Target
		if (Wood.Position - Char.HumanoidRootPart.Position).magnitude <10 then
			Gather = false
			RG = Player.PlayerGui.Gathering
			RG.BG.Visible = true
			Char.Humanoid.WalkSpeed = 0
			ShowProgress('Tree')
			Char.Humanoid.WalkSpeed = 16

			for i,v in pairs(Wood.Parent.Leaves:GetChildren()) do
				if v:IsA('Part') then
					v.Anchored = false
				end
			end

			Wood:Destroy()
			--Drop Wood
			RG.BG.Visible = false
			Gather = true
		end
	end

5 comments

0

Provide an image of the explorer, please. (Of the tree leaves' children.)

0

Uh, are this is the full script, or there any errors?

0

I don't think it's the loop. It could be the magnitude condition not passing.

0

its only the error part

0

I added tree model structure's photo as well

1 answer

✓ Accepted answer

I don't think you should put...

for i,v in pairs(Wood.Parent.Leaves:GetChildren()) do
	if v:IsA('Part') then
		v.Anchored = false
	end
end

Use...

local leaves = Wood.Parent.Leaves:GetChildren()

for i = 1, #leaves do
	if leaves[i]:IsA('BasePart') then -- I prefer using BasePart instead of just Part as it includes Unions.
		leaves[i].Anchored = false
	end
end

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