I need help with a script for chopping trees

I would like to achieve a script that can be parented by a model and drop the wood tools on the ground. I would like someone to help me with this script.

For some reason, it does not work. I would like it to be able to be parented by a model. Please help me with this.

It should go in the player’s inventory. I am checking if the Machete is activated when it touches the tree.

I would like all the parts to be affected at once.
Please make it so I do not have to rename anything.
It is in the model of the wood in the tree. It is not a union and I would not like to union about 5000 parts.
I would like all the parts to be chopped at the same time.
Do not edit the leaves

Here is a image and the script:

Screenshot 2020-05-19 at 11.19.54 PM

logs = script.Parent:GetChildren()
script.Parent.Touched:Connect(function(hit)
	if hit.parent.name == "Machete" then
		if hit.parent.Activated:Connect (TreeWood) then
		end
		
		function TreeWood()
		if logs.Transparency == 0 then
		logs.CanCollide = false
		logs.Transparency = 0.5
		local clone = game.ReplicatedStorage.ToolsDrops.Wood:Clone()
    clone.Parent = game.Players.LocalPlayer.Backpack
		wait(10)
		script.Parent.CanCollide = true
		script.Parent.Transparency = 0
	end
	end
	end
	end)

Please give me some more details of what you are trying to do, when a Machete touches a tree, it should drop wood at the current postion of the torso of the player? But you are checking if Machete is touching it? Wouldn’t this script belong in the tree?

1 Like

It should drop near the tree or it should go in the player’s inventory. I am checking if the Machete is activated when it touches the tree.

1 Like

Where is your script in? The axe, the tree or the player?

1 Like

The tree. I would like all the parts to be chopped at the same time.

1 Like

In order to get all the parts, you can use the :GetChildren() function and use a loop.

It would like something like:

local Tree = game.Workspace.Tree
for i, v in pairs(Tree.Stump:GetChildren()) do -- GetChildren() returns a table, and we can use a loop to iterate through it
    if v:IsA("BasePart") then -- make sure it's a part and not the script
        -- whatever you want to do with the part (v)
    end
end
1 Like

Where would I insert this in my script? I recently started scripting so I do not know where to insert it.

You could just implement the loop into the script you already have.

Where would I insert it inside the code of my script?

Do I do it like this?
``lua
logs = script.Parent:GetChildren()
local Tree = script.Parent.Parent
script.Parent.Touched:Connect(function(hit)
if hit.parent.name == “Machete” then
if hit.parent.Activated:Connect (TreeWood) then
end

	function TreeWood()
	if logs.Transparency == 0 then
for i, v in pairs(Tree.Stump:GetChildren()) do -- GetChildren() returns a table, and we can use a loop to iterate through it
if v:IsA("BasePart") then -- make sure it's a part and not the script
    -- whatever you want to do with the part (v)
logs.CanCollide = false
	logs.Transparency = 0.5
	local clone = game.ReplicatedStorage.ToolsDrops.Wood:Clone()
clone.Parent = game.Players.LocalPlayer.Backpack
	wait(10)
	script.Parent.CanCollide = true
	script.Parent.Transparency = 0
end
end
end
end
end
end)

Test it out and see the results.

Hey, I just saw your workspace image,

So you have all the trees in the stumps model?

local stump = script.Parent
local children = stump:GetChildren()

for i, v in pairs(children) do
    if v:IsA("BasePart") then
        v.Touched:Connect(function(hit)
            if hit.Parent.Name == "Machete" then
                -- I don't know what you want to do to the logs so just put that here
            end
        end)
    end
end
1 Like