Clone doesnt get destroyed

Hi, my script doesnt destroy the clone variable (is a textlabel) but it prints the clone destroyed line. The if game.ReplicatedStorage.Eng1OnFire.Value == true then line works. But the elseif game.ReplicatedStorage.Eng1OnFire.Value == false then line doesnt. Any help?

game.ReplicatedStorage.Eng1OnFire.Changed:Connect(function()
	local clone = script.Parent.Msg:Clone()
	if game.ReplicatedStorage.Eng1OnFire.Value == true then
		clone.Text = 'Eng 1 Fire'
		clone.Parent = script.Parent.Failures.Frame
		clone.TextColor = BrickColor.Red()
	elseif game.ReplicatedStorage.Eng1OnFire.Value == false then
		clone:Destroy()
		print('clone destroyed')
	end
end)



i suggest using Debris instead.

local Debris = game:GetService("Debris")
Debris:AddItem(YourInstance,1) -- First Arg is the Instance, Second is the Wait time

also your cloneObject will clone each time the Eng1OnFire Object Value changes.

The variable is not stored outside the function, so when you run the if false statement, it won’t destroy the previous textlabel.

Try this instead:

local clone = script.Parent.Msg:Clone()

game.ReplicatedStorage.Eng1OnFire.Changed:Connect(function()
	if game.ReplicatedStorage.Eng1OnFire.Value == true then
		clone.Text = 'Eng 1 Fire'
		clone.Parent = script.Parent.Failures.Frame
		clone.TextColor = BrickColor.Red()
	elseif game.ReplicatedStorage.Eng1OnFire.Value == false then
		clone:Destroy()
		print('clone destroyed')
	end
end)
1 Like

I don’t believe Debris would fit for this situation, especially since he’s changing a value to destroy these objects.

There are multiple of them, I just posted 1 one them. Wont they all need to use different clones?

game.ReplicatedStorage.Eng1OnFire.Changed:Connect(function()
	local clone = script.Parent.Msg:Clone()
	if game.ReplicatedStorage.Eng1OnFire.Value == true then
		clone.Text = 'Eng 1 Fire'
		clone.Parent = script.Parent.Failures.Frame
		clone.TextColor = BrickColor.Red()
	elseif game.ReplicatedStorage.Eng1OnFire.Value == false then
		clone.Parent = game.ReplicatedFirst
		print('clone destroyeed')
	end
end)
game.ReplicatedStorage.Eng2OnFire.Changed:Connect(function()
	local clone = script.Parent.Msg:Clone()

	if game.ReplicatedStorage.Eng2OnFire.Value == true then
		clone.Text = 'Eng 2 Fire'
		clone.Parent = script.Parent.Failures.Frame
		clone.TextColor = BrickColor.Red()
	elseif game.ReplicatedStorage.Eng2OnFire.Value == false then
		clone:Destroy()
	end
end)
game.ReplicatedStorage.APUOnFire.Changed:Connect(function()
	local clone = script.Parent.Msg:Clone()
	if game.ReplicatedStorage.APUOnFire.Value == true then
		clone.Text = 'APU Fire'
		clone.Parent = script.Parent.Failures.Frame
		clone.TextColor = BrickColor.Red()

	elseif game.ReplicatedStorage.APUOnFire.Value == false then
		clone:Destroy()
	end
end)

Debris:AddItem(clone,1)
also i don’t think he needs to clone the ClonedMsg Object only once.

Just move those clone variables outside of their respective functions and name them differently.

If you need to clone the items more than once you need to change your logic. I can do that for you if you give me the script.

This works perfectly, but when it gets called again I get this error

Okay yea, you will have to change your logic, could you give me your entire script for that section?


local clone = script.Parent.Msg:Clone()
local clone2 = script.Parent.Msg:Clone()
local clone3 = script.Parent.Msg:Clone()

game.ReplicatedStorage.Eng1OnFire.Changed:Connect(function()
	if game.ReplicatedStorage.Eng1OnFire.Value == true then
		clone.Text = 'Eng 1 Fire'
		clone.Parent = script.Parent.Failures.Frame
		clone.TextColor = BrickColor.Red()
	elseif game.ReplicatedStorage.Eng1OnFire.Value == false then
		clone:Destroy()
		print('clone destroyed')
	end
end)

game.ReplicatedStorage.Eng2OnFire.Changed:Connect(function()

	if game.ReplicatedStorage.Eng2OnFire.Value == true then
		clone2.Text = 'Eng 2 Fire'
		clone2.Parent = script.Parent.Failures.Frame
		clone2.TextColor = BrickColor.Red()
	elseif game.ReplicatedStorage.Eng2OnFire.Value == false then
		clone2:Destroy()
	end
end)
game.ReplicatedStorage.APUOnFire.Changed:Connect(function()
	local clone = script.Parent.Msg:Clone()
	if game.ReplicatedStorage.APUOnFire.Value == true then
		clone3.Text = 'APU Fire'
		clone3.Parent = script.Parent.Failures.Frame
		clone3.TextColor = BrickColor.Red()

	elseif game.ReplicatedStorage.APUOnFire.Value == false then
		clone3:Destroy()
	end
end)


Okay, this should work:

--//Services
local ReplicatedStorage = game:GetService("ReplicatedStorage")

--//Variables
local Engine1 = ReplicatedStorage.Eng1OnFire
local Engine2 = ReplicatedStorage.Eng2OnFire
local APU = ReplicatedStorage.APUOnFire

--//Initialization
local clone = script.Parent.Msg:Clone()
local clone2 = script.Parent.Msg:Clone()
local clone3 = script.Parent.Msg:Clone()

--//Functions
Engine1:GetPropertyChangedSignal("Value"):Connect(function()
	if Engine1.Value then
		if not clone then
			clone = script.Parent.Msg:Clone()
		end
		
		clone.Text = 'Eng 1 Fire'
		clone.TextColor = BrickColor.Red()
		clone.Parent = script.Parent.Failures.Frame
	else
		clone:Destroy()
		clone = nil
	end
end)

Engine2:GetPropertyChangedSignal("Value"):Connect(function()
	if Engine2.Value then
		if not clone2 then
			clone2 = script.Parent.Msg:Clone()
		end
		
		clone2.Text = 'Eng 2 Fire'
		clone2.TextColor = BrickColor.Red()
		clone2.Parent = script.Parent.Failures.Frame
	else
		clone2:Destroy()
		clone2 = nil
	end
end)

APU:GetPropertyChangedSignal("Value"):Connect(function()
	if APU.Value then
		if not clone3 then
			clone3 = script.Parent.Msg:Clone()
		end
		
		clone3.Text = 'APU Fire'
		clone3.TextColor = BrickColor.Red()
		clone3.Parent = script.Parent.Failures.Frame
	else
		clone3:Destroy()
		clone3 = nil
	end
end)

(I also polished your script a bit and made it more readable)

Works like a charm! Thanks for you help

1 Like

No problem. If you need any more help, feel free to ask.