Cloning problem

Im trying to make a script so when you finish chopping it, it deletes itself and then after some time goes back to orginal state, But when it does go back to its orginal state it gets the mesh of a different tree? There are no errors


if health.Value == 0 or health.Value < 0 then
	workspace.Treehit:Play()
		print(health.Value)
		wodas()
	    tree.Tree.Anchored = false
		tree.Tree.CanTouch = false
		goi.CanvasGroup.Visible = false
		goi.CanvasGroup.Amount.Size = UDim2.new(0,0,0,75)
		amount:Destroy()
		game.ReplicatedStorage.AddWood:FireServer()
		local newtree = hit.Parent -getting oldtree
		tree:Destroy() -deling old tree
		wait(2)
		newtree:Clone() -cloning tree
		newtree = workspace
		newtree.Tree.Tree.CFrame = CFrame.new(oldpos)
		newtree.Tree.Tree.Anchored = true
		newtree.Tree.Tree.CanTouch = true
		newtree.Tree.Health.Value = newtree.Tree.Maxhealth.Value
		newtree.Tree.DamageDelt.Value =0
		db = true
		return
	end

Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.

2 Likes

I’m pretty sure it would be easier if you cloned the newtree before deleting the destroyed tree?
So, you clone it, and hide it since collisions are already disabled. This might save you time from generating new trees.

1 Like

How would I hide the tree before it gets deleted?

1 Like

A little change could do the trick:

...
local newtree = hit.Parent:Clone()
newtree.Parent = hit.Parent -- recommended to set parent of cloned item again
newtree.Transparency = 1
-- or use a for loop if there are dependencies, basiacally hide it from the world
tree:Destroy();
wait(2)
newtree.Transparency = 0;
-- or for loop to make it visible after the old tree is destroyed
1 Like

is there a way to do getchildren and check if it is meshpart and ifso then make the transparency = 1?

1 Like

Yup, it’s as simple as that:

for _, mesh in pairs(tree:GetChildren()) do
	if mesh:IsA("MeshPart") and mesh.Transparency == 1 then
		-- it's a mesh with transparency 1
	end
end
1 Like

thanks for the help, but it still does not keep the same mesh and go to another to a different tree and not the same one

1 Like

Is there anything that randomizes the mesh id, or actual mesh object?
Script on the tree, other terrain manager or etc?

1 Like

no there is not something like taht

1 Like

I don’t know what causes it then. Try storing the mesh id(s) and apply them again after cloning

1 Like

Would, tre problem be because it is in a model? and the model name is the same as every other tree?

1 Like

Yep. That’s definitely issue. Either rename the tree when destroyed, or use the command line to number the models.

1 Like

my script only works iof the name is tree

if hit.Parent.Name == "Tree"and db == true  and canSwing == false and hit.Parent.Canbeattacked.Value == false then
1 Like

I mean… Try to get a workaround or accept randomized meshes.

1 Like

how would acept randomized meshes?

1 Like

You have a problem that the new tree isn’t the same as the previous tree.
You need to change your script a bit to fix the issue or don’t do anything? It’s do or don’t, your choice though.

1 Like

Ok, i think i found the problem, is there anther way to get the tree?

local tree = hit.Parent
1 Like

As I said above, it’s recommended to bulk rename all the trees with a studio command line script, or rename the tree after it is destroyed - which can still cause problems even after you rename it in game.
Renaming is simple following this sample:

for n, tree in pairs(workspace:Getchildren()) do if tree.Name == "Tree" then tree.Name = "Tree"..tostring(n) end end

This will add a “unique” number to every of your trees so there won’t be any confusion

1 Like