Cloned object gets destroyed

I wrote a spaghetti code to clone a uı animation into the chat tag

When the object is cloned it imindeantly gets destroyed

ı have tried for 8 hours but couldnt achieve it so here you go

code

local clientChatModules = script.Parent.Parent
local ChatSettings = require(clientChatModules:WaitForChild("ChatSettings"))
local ChatConstants = require(clientChatModules:WaitForChild("ChatConstants"))
local util = require(script.Parent:WaitForChild("Util"))


function CreateMessageLabel(messageData, channelName)

	local fromSpeaker = messageData.FromSpeaker
	local message = messageData.Message

	local extraData = messageData.ExtraData or {}
	local useFont = extraData.Font or ChatSettings.DefaultFont
	local useTextSize = extraData.TextSize or ChatSettings.ChatWindowTextSize
	local usePrefix = extraData.Prefix or ChatSettings.DefaultPrefix
	local usePrefixColor = extraData.PrefixColor or ChatSettings.DefaultPrefixColor
	local useNameColor = extraData.NameColor or ChatSettings.DefaultNameColor
	local useChatColor = extraData.ChatColor or ChatSettings.DefaultChatColor
	local useChannelColor = extraData.ChannelColor or ChatSettings.DefaultMessageColor

	local formatPrefix = string.format("%s", usePrefix)
	local formatUseName = string.format("[%s]:", fromSpeaker)
	local speakerNameSize = util:GetStringTextBounds(formatUseName, useFont, useTextSize)
	local numNeededSpaces = util:GetNumberOfSpaces(formatPrefix, useFont, useTextSize) + util:GetNumberOfSpaces(formatUseName, useFont, useTextSize) + 1

	local BaseFrame, BaseMessage = util:CreateBaseMessage("", useFont, useTextSize, useChatColor)
	local NameButton = util:AddNameButtonToBaseMessage(BaseMessage, useNameColor, formatUseName, fromSpeaker)
	local PrefixLabel = util:AddPrefixLabelToBaseMessage(BaseMessage, usePrefixColor, formatPrefix)
	local ChannelButton = nil
	
> 	spawn(function()
> 		if PrefixLabel.Text == "[NITRO BOOSTER] " then
> 			local h = 0
> 			while PrefixLabel and PrefixLabel.Parent do
> 				local clonepart = workspace.Model
> 				clonepart:Clone()
> 				clonepart.Parent = PrefixLabel
> 				h = h + 1
> 				if h > 255 then
> 					h = 0
> 					local ac = workspace.Part
> 					ac:Clone()
> 					ac.Parent = workspace
> 				end
> 				game:GetService("RunService").Heartbeat:wait()
> 			end
> 
> 		end
> 	end)

	if channelName ~= messageData.OriginalChannel then
		local formatChannelName = string.format("{%s}", messageData.OriginalChannel)
		ChannelButton = util:AddChannelButtonToBaseMessage(BaseMessage, useChannelColor, formatChannelName, messageData.OriginalChannel)
		PrefixLabel.Position = UDim2.new(0, ChannelButton.Size.X.Offset, 0, 0)
		NameButton.Position = UDim2.new(0, ChannelButton.Size.X.Offset + PrefixLabel.Size.X.Offset, 0, 0)
		numNeededSpaces = numNeededSpaces + util:GetNumberOfSpaces(formatChannelName, useFont, useTextSize)
	else
		PrefixLabel.Position = UDim2.new(0, 0, 0, 0)
		NameButton.Position = UDim2.new(0, PrefixLabel.Size.X.Offset, 0, 0)
	end

	local function UpdateTextFunction(messageObject)
		if messageData.IsFiltered then
			BaseMessage.Text = string.rep(" ", numNeededSpaces) .. messageObject.Message
		else
			BaseMessage.Text = string.rep(" ", numNeededSpaces) .. string.rep("_", messageObject.MessageLength)
		end
	end

	UpdateTextFunction(messageData)

	local function GetHeightFunction(xSize)
		return util:GetMessageHeight(BaseMessage, BaseFrame, xSize)
	end

	local FadeParmaters = {}
	FadeParmaters[PrefixLabel] = {
		TextTransparency = {FadedIn = 0, FadedOut = 1},
		TextStrokeTransparency = {FadedIn = 0.75, FadedOut = 1}
	}
	
	FadeParmaters[NameButton] = {
		TextTransparency = {FadedIn = 0, FadedOut = 1},
		TextStrokeTransparency = {FadedIn = 0.75, FadedOut = 1}
	}

	FadeParmaters[BaseMessage] = {
		TextTransparency = {FadedIn = 0, FadedOut = 1},
		TextStrokeTransparency = {FadedIn = 0.75, FadedOut = 1}
	}

	if ChannelButton then
		FadeParmaters[ChannelButton] = {
			TextTransparency = {FadedIn = 0, FadedOut = 1},
			TextStrokeTransparency = {FadedIn = 0.75, FadedOut = 1}
		}
	end

	local FadeInFunction, FadeOutFunction, UpdateAnimFunction = util:CreateFadeFunctions(FadeParmaters)

	return {
		[util.KEY_BASE_FRAME] = BaseFrame,
		[util.KEY_BASE_MESSAGE] = BaseMessage,
		[util.KEY_UPDATE_TEXT_FUNC] = UpdateTextFunction,
		[util.KEY_GET_HEIGHT] = GetHeightFunction,
		[util.KEY_FADE_IN] = FadeInFunction,
		[util.KEY_FADE_OUT] = FadeOutFunction,
		[util.KEY_UPDATE_ANIMATION] = UpdateAnimFunction
	}
end

return {
	[util.KEY_MESSAGE_TYPE] = ChatConstants.MessageTypeDefault,
	[util.KEY_CREATOR_FUNCTION] = CreateMessageLabel
}

Do this:

ac:Clone().Parent = --parent

???

spawn(function()
	if PrefixLabel.Text == "[NITRO BOOSTER] " then
		local h = 0
		while PrefixLabel and PrefixLabel.Parent do
			local clonepart = workspace.Model
			clonepart:Clone()
			clonepart.Parent = PrefixLabel
			h = h + 1
			if h > 255 then
				h = 0
				local ac = workspace.Part
				ac:Clone()
				ac.Parent = workspace
			end
			game:GetService("RunService").Heartbeat:wait()
		end
	end
end)

I’m rather confused on why it is destroying itself after it was cloned. The rest of the code doesn’t help much either…

local PrefixLabel = util:AddPrefixLabelToBaseMessage(BaseMessage, usePrefixColor, formatPrefix)
1 Like

Try adding a wait like this

spawn(function()
	if PrefixLabel.Text == "[NITRO BOOSTER] " then
		local h = 0
		while PrefixLabel and PrefixLabel.Parent do
            wait()
			local clonepart = workspace.Model
			clonepart:Clone()
			clonepart.Parent = PrefixLabel
			h = h + 1
			if h > 255 then
				h = 0
				local ac = workspace.Part
				ac:Clone()
				ac.Parent = workspace
			end
			game:GetService("RunService").Heartbeat:wait()
		end
	end
end)

it doesnt matter if you clone it multiple times or parent it on something else no matter what you clone it gets cloned for a split second than it gets destroyed.

finaly found a solution

spawn(function()
	if PrefixLabel.Text == "[NITRO BOOSTER] " then
		local h = 0
		while PrefixLabel and PrefixLabel.Parent do
			local clonepart = workspace.Model
			local cloning = clonepart:Clone()
			cloning.Parent = PrefixLabel
			h = h + 1
			if h > 255 then
				h = 0
				local ac = workspace.Part
				ac:Clone()
				ac.Parent = workspace
			end
			game:GetService("RunService").Heartbeat:wait()
		end

	end
end)