How do I make this part clone itself after being destroyed?

This script adds a part when I type “add”, and removes it when I type “remove”
The problem is, I can’t re-add the part when typing “add” after it has been removed.

text.FocusLost:Connect(function(enterPressed)
	if not enterPressed or text.Text ~= "add" then
		return
	end
	
	part:Clone()
	part.Parent = game.Workspace
	tweenAdd:Play()
end)

text.FocusLost:Connect(function(enterPressed)
	if not enterPressed or text.Text ~= "remove" then
		return
	end

	tweenRemove:Play()
	wait(0.6)
	part:Destroy()
end)

Any way to fix this?

1 Like

I think you’re clonning the part and parenting the original to workspace and then destroying the original, try this:

local c

text.FocusLost:Connect(function(enterPressed)
if not enterPressed or text.Text ~= “add” then
return
end

 c = part:Clone()
c.Parent = game.Workspace
tweenAdd:Play()

end)

text.FocusLost:Connect(function(enterPressed)
if not enterPressed or text.Text ~= “remove” then
return
end

tweenRemove:Play()
wait(0.6)
c:Destroy()

end)

2 Likes

Doesn’t work, and the tweens stopped working for some reason.
Here is the entire script, if it helps.

local text = script.Parent
local TweenService = game:GetService("TweenService")

local origin = Vector3.new(0, 0, 0)

local part = Instance.new("Part")
part.Anchored = true
part.Color = Color3.new(0, 1, 0)
part.Material = Enum.Material.Neon
part.Position = origin
part.Size = Vector3.new(3, 0.1, 3)
part.Transparency = 0

local goalAdd = {}
goalAdd.Color = Color3.new(1, 1, 1)
goalAdd.Size = Vector3.new(2, 0.1, 2)

local goalRemove = {}
goalRemove.Color = Color3.new(1, 0, 0)
goalRemove.Size = Vector3.new(1.5, 0.1, 1.5)
goalRemove.Transparency = 1

local tweenInfoAdd = TweenInfo.new(1, Enum.EasingStyle.Cubic, Enum.EasingDirection.Out)
local tweenInfoRemove = TweenInfo.new(0.5, Enum.EasingStyle.Cubic, Enum.EasingDirection.In)

local tweenAdd = TweenService:Create(part, tweenInfoAdd, goalAdd)
local tweenRemove = TweenService:Create(part, tweenInfoRemove, goalRemove)

text.FocusLost:Connect(function(enterPressed)
	if not enterPressed or text.Text ~= "add" then
		return
	end
	
	part:Clone()
	part.Parent = game.Workspace
	tweenAdd:Play()
end)

text.FocusLost:Connect(function(enterPressed)
	if not enterPressed or text.Text ~= "remove" then
		return
	end

	tweenRemove:Play()
	wait(0.6)
	part:Destroy()
end)
2 Likes

try this:

local text = script.Parent
local TweenService = game:GetService("TweenService")

local c

local origin = Vector3.new(0, 0, 0)

local part = Instance.new("Part")
part.Anchored = true
part.Color = Color3.new(0, 1, 0)
part.Material = Enum.Material.Neon
part.Position = origin
part.Size = Vector3.new(3, 0.1, 3)
part.Transparency = 0

local goalAdd = {}
goalAdd.Color = Color3.new(1, 1, 1)
goalAdd.Size = Vector3.new(2, 0.1, 2)

local goalRemove = {}
goalRemove.Color = Color3.new(1, 0, 0)
goalRemove.Size = Vector3.new(1.5, 0.1, 1.5)
goalRemove.Transparency = 1

local tweenInfoAdd = TweenInfo.new(1, Enum.EasingStyle.Cubic, Enum.EasingDirection.Out)
local tweenInfoRemove = TweenInfo.new(0.5, Enum.EasingStyle.Cubic, Enum.EasingDirection.In)



text.FocusLost:Connect(function(enterPressed)
	if not enterPressed or text.Text ~= "add" then
		return
	end
	
	c = part:Clone()
	c.Parent = game.Workspace
	local tweenAdd = TweenService:Create(c, tweenInfoAdd, goalAdd)
tweenAdd:Play()
end)

text.FocusLost:Connect(function(enterPressed)
	if not enterPressed or text.Text ~= "remove" then
		return
	end

	local tweenRemove = TweenService:Create(c, tweenInfoRemove, goalRemove)
	wait(0.6)
	c:Destroy()
end)
2 Likes

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