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.
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)
The problem was that you did not disconnect the .Touched connection, so each time you were swinging it was:
Non stop creating logs each time the handle was touched
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)