:Clone() Clones Twice

Hello, I’m trying to make a system that when a tool called “Axe” touches a tree it will fall and turn into 1 log.

Here is the script used in the “Axe” tool:

local log1 = game.ServerStorage.Logs[“Tree Log 1 Folder”][“Tree Log 1”]

script.Parent.Touched:Connect(function(Hit)
if Hit.Name == ‘Tree 1’ then
Hit.Anchored = false
wait(5)
Hit.Transparency = 0.1
wait(0.1)
Hit.Transparency = 0.2
wait(0.1)
Hit.Transparency = 0.3
wait(0.1)
Hit.Transparency = 0.4
wait(0.1)
Hit.Transparency = 0.5
wait(0.1)
Hit.Transparency = 0.6
wait(0.1)
Hit.Transparency = 0.7
wait(0.1)
Hit.Transparency = 0.8
wait(0.1)
Hit.Transparency = 0.9
wait(0.1)
Hit.Transparency = 1
wait(1)
Hit:Destroy()
log1:Clone().Parent = workspace
wait(23)
game.ServerStorage.Trees[“Tree 1”]:Clone().Parent= workspace
end
end)

Thanks for the help! :slight_smile:

1 Like

I suggest implementing a debounce mechanism to prevent multiple cloning of the tree when the BasePart.Touched event is triggered multiple times. For a more comprehensive understanding of debounces, I recommend referring to this informative article.

1 Like

Have you tried adding a Debounce or a Cooldown?

holy moly that is messy but anyways you most likely just need a debounce

I added debounce for you and simplified your script, hopefully this will help you

local TweenService = game:GetService("TweenService")
local tree1 = game.ServerStorage.Trees["Tree 1"]
local log1 = game.ServerStorage.Logs["Tree Log 1 Folder"]["Tree Log 1"]

local db = {}

script.Parent.Touched:Connect(function(hit)
    if hit.Name ~= "Tree 1" then return end
    
    local tree = hit:FindFirstAncestorOfClass("Model")
    if not tree or db[tree] then return end
    
    db[tree] = true
    local pos = tree:GetPivot()
    
    for _, part in tree:GetDescendants() do
        if part:IsA("BasePart") then
            part.Anchored = false
            TweenService:Create(part, TweenInfo.new(1), {Transparency = 1}):Play()
        end
    end
    
    task.wait(1)
    tree:Destroy()
    
    local log = log1:Clone()
    log:PivotTo(pos)
    log.Parent = workspace
    
    task.wait(23)
    
    local newTree = tree1:Clone()
    newTree:PivotTo(pos)
    newTree.Parent = workspace
end)
3 Likes

Sorry if it’s a bit messy I’m still sort of a rookie at lua and don’t know much of the advanced scripting.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.