Tree cutting script does not unrecognise a cut tree

so, i try to create a system in which the ax when you click it will cut the tree and it will drop wood to the ground

So, everything works fine, but the problem is that the ax does not stop recognizing the tree and does not stop throwing wood like crazy, and I need help so that the ax stops recognizing the tree and the pieces come out randomly.

the output:
image

and the game:

i try to do a cooldown in the tool so that it is deactivated but it does not work because when it is activated again this happens again

Script in the axe (its a normal script)

tool = script.Parent
handle = tool:WaitForChild("Handle")

tool.Activated:Connect(function()
	print ("The tool has been clicked") -- this print the actived tool
	handle.Trail.Enabled = true
	wait(0.5)    ---- enable a disabled a effect :3 
	handle.Trail.Enabled = false
	
   handle.Touched:Connect(function(hit)
            if hit.Parent.Name == "Trunk" then
             print("omg A tree") -- print when find a tree
        local newitem = game.ServerStorage.Items:FindFirstChild("Logs"):Clone() -- drop logs
		newitem.Parent = game.Workspace.Items
		newitem.Position = hit.Position -- select the log drop place
		else
	    print("WhatAreUdoingMan") -- no trees found :(
            end
        end)
   end)

Insert a TouchEnded event :wink: , that will stop running a code when the ‘touch’ has been stopped.

Could you give me an idea of how to apply this?, because I am trying to think but nothing comes to mind

Using else on a Touched event won’t do much in the way you present it.

Use:

handle.TouchEnded:connect(function(hit)
-- Code here
end)
1 Like

The problem was that you did not disconnect the .Touched connection, so each time you were swinging it was:

  1. Non stop creating logs each time the handle was touched
  2. Creating ANOTHER .Touched connection each swing, so after 4 swings there would’ve been x4 logs being created per .Touched fire.

I have improved your code as well as fixed your initial problem.

local tool = script.Parent
local handle = tool:WaitForChild(“Handle”)

local swinging = false
function Swing()
	if swinging then return end -- cannot swing while already swinging
	
	swinging = true
	
	handle.Trail.Enabled = true
	
	local con
	con = handle.Touched:Connect(function(hit)
        if hit.Parent.Name == "Trunk" then
         print("omg A tree") -- print when find a tree
    		local newitem = game.ServerStorage.Items:FindFirstChild("Logs"):Clone() -- drop logs
			newitem.Parent = game.Workspace.Items
			newitem.Position = hit.Position -- select the log drop place
			
			con:Disconnect() -- disconnect the connection!
		else
			print("WhatAreUdoingMan") -- no trees found :(
        end
    end)

	wait(0.5)
	
	con:Disconnect() -- disconnect the connection!
	handle.Trail.Enabled = false
	swinging = false -- end of swing, now you can swing again
end

tool.Activated:Connect(function()
	print ("The tool has been clicked") -- this print the actived tool
	Swing()

end)
1 Like

Thanks so much u Save me bro its work

1 Like