Help with tree destroying script

Hello. My tree destroying script only destroys one part. Here’s the code.

local health = 10
local db = false

    game.Players.PlayerAdded:Connect(function(player)
    	for _,v in pairs(script.Parent:GetChildren()) do
    		if v:IsA("Part") then
    			v.Touched:Connect(function(hit)
    				if hit.Parent:IsA("Tool") then
    					if not db then
    						db = true
    						health -= 1
    						print(health)	
    						if health <= 0 then
    							v.Anchored = false
    							task.wait(1)
    							v:Destroy()
    							player.leaderstats.Tokens.Value += 15							
    						end	
    						task.wait(1)
    						db = false
    					end
    				end
    			end)
    		end
    	end
    end)

It is only destroying the part that is being touched you need to destroy the tree model.

1 Like

Try this make sure to set the MyTreeModel variable to your tree.

local health = 10
local db = false
local MyTreeModel = MyTreeModel 

    game.Players.PlayerAdded:Connect(function(player)
    	for _,v in pairs(script.Parent:GetChildren()) do
    		if v:IsA("Part") then
    			v.Touched:Connect(function(hit)
    				if hit.Parent:IsA("Tool") then
    					if not db then
    						db = true
    						health -= 1
    						print(health)	
    						if health <= 0 then
    							v.Anchored = false
    							task.wait(1)
    							MyTreeModel :Destroy()
    							player.leaderstats.Tokens.Value += 15							
    						end	
    						task.wait(1)
    						db = false
    					end
    				end
    			end)
    		end
    	end
    end)

Your script makes the part that I hit unanchored, then destroys it immediately after. I want all of the parts to be unanchored then destroy it after a second.

Try this:

local health = 10
local db = false
local MyTreeModel = MyTreeModel

game.Players.PlayerAdded:Connect(function(player)
	for _,v in pairs(script.Parent:GetChildren()) do
		if v:IsA("Part") then
			v.Touched:Connect(function(hit)
				if hit.Parent:IsA("Tool") then
					if not db then
						db = true
						health -= 1
						print(health)	
						if health <= 0 then
							for _,Parts in pairs(MyTreeModel:GetChildren()) do
								v.Anchored = false
							end
							task.wait(1)
							MyTreeModel :Destroy()
							player.leaderstats.Tokens.Value += 15							
						end	
						task.wait(1)
						db = false
					end
				end
			end)
		end
	end
end)

local health = 10
local db = false

game.Players.PlayerAdded:Connect(
    function(player)
        for _, v in pairs(script.Parent:GetChildren()) do
            if v:IsA("Part") then
                v.Touched:Connect(
                    function(hit)
                        if hit.Parent:IsA("Tool") then
                            if not db then
                                db = true
                                health = health - 1
                                print(health)
                                if health <= 0 then
                                    for a, b in pairs(script.Parent:GetChildren()) do
                                        v.Anchored = false
                                        local c =
                                            coroutine.create(
                                            function()
                                                task.wait(1)
                                                v:Destroy()
                                            end
                                        )
                                    coroutine.resume(c)
                                    end
                                    player.leaderstats.Tokens.Value = player.leaderstats.Tokens.Value + 15
                                end
                                task.wait(1)
                                db = false
                            end
                        end
                    end
                )
            end
        end
    end
)

try this