: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:

3 Likes

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?

1 Like

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

I added debounce for you and simplified/optimized (idk) your script, Hopefully this will help you.

Open me
local tree1 = game.ServerStorage.Trees["Tree 1"]
local log1 = game.ServerStorage.Logs["Tree Log 1 Folder"]["Tree Log 1"]
local db = false

script.Parent.Touched:Connect(function(hit)
	if db or not hit:IsA("Model") or hit.Name ~= "Tree 1" then
		return
	end

	db = true

	hit.Anchored = false

	task.spawn(function()
		for transparency = 0.1, 1, 0.1 do
			task.wait(0.1)
			hit.Transparency = transparency
		end

		hit:Destroy()
		log1:Clone().Parent = workspace

		task.wait(23)

		tree1:Clone().Parent = workspace
		db = false
	end)
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.

2 Likes

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