local function F(F,u,A)if FFlagUserChatNewMessageLengthCheck2 then return u+(A-u)*math.min(getMessageLength(F)/75.0,1.0)else return u+(A-u)*math.min(string.len(F)/75.0,1.0)end end local function u()local F={}F.data={}local u=Instance.new("\066\105\110\100\097\098\108\101\069\118\101\110\116")F.Emptied=u.Event function F.Size(u)return#F.data end function F.Empty(u)return F:Size()<=0 end function F.PopFront(A)table.remove(F.data,1)if F:Empty()then u:Fire()end end function F.Front(u)return F.data[1]end function F.Get(A,u)return F.data[u]end function F.PushBack(A,u)table.insert(F.data,u)end function F.GetData(u)return F.data end return F end local function A()local F={}F.Fifo=u()F.BillboardGui=nil return F end local function l()local F={}F.data={}local u=0 function F.Size(F)return u end function F.Erase(l,A)if F.data[A]then u=u-1 end F.data[A]=nil end function F.Set(I,A,l)F.data[A]=l if l then u=u+1 end end function F.Get(l,u)if not u then return end if not F.data[u]then F.data[u]=A()local l=nil l=F.data[u].Fifo.Emptied:connect(function()l:disconnect()F:Erase(u)end)end return F.data[u]end function F.GetData(u)return F.data end return F end local function I(u,A,l)local I={}function I.ComputeBubbleLifetime(l,u,A)if A then return F(u,8,15)else return F(u,12,20)end end I.Origin=nil I.RenderBubble=nil I.Message=u I.BubbleDieDelay=I:ComputeBubbleLifetime(u,l)I.BubbleColor=A I.IsLocalPlayer=l return I end local function H(F,u,A)local l=I(u,BubbleColor.WHITE,A)if F then l.User=F.Name l.Origin=F.Character end return l end local function i(F,u,A,l)local H=I(u,l,A)H.Origin=F return H end function createChatBubbleMain(F,u)local A=Instance.new("\073\109\097\103\101\076\097\098\101\108")A.Name="\067\104\097\116\066\117\098\098\108\101"A.ScaleType=Enum.ScaleType.Slice A.SliceCenter=u A.Image="\114\098\120\097\115\115\101\116\058\047\047\116\101\120\116\117\114\101\115\047"..(tostring(F).."\046\112\110\103")A.BackgroundTransparency=1 A.BorderSizePixel=0 A.Size=UDim2.new(1.0,0,1.0,0)A.Position=UDim2.new(0,0,0,0)return A end function createChatBubbleTail(F,u)local A=Instance.new("\073\109\097\103\101\076\097\098\101\108")A.Name="\067\104\097\116\066\117\098\098\108\101\084\097\105\108"A.Image="\114\098\120\097\115\115\101\116\058\047\047\116\101\120\116\117\114\101\115\047\117\105\047\100\105\097\108\111\103\095\116\097\105\108\046\112\110\103"A.BackgroundTransparency=1 A.BorderSizePixel=0 A.Position=F A.Size=u return A end function createChatBubbleWithTail(F,u,A,l)local I=createChatBubbleMain(F,l)local H=createChatBubbleTail(u,A)H.Parent=I return I end function createScaledChatBubbleWithTail(F,u,A,l)local I=createChatBubbleMain(F,l)local H=Instance.new("\070\114\097\109\101")H.Name="\067\104\097\116\066\117\098\098\108\101\084\097\105\108\070\114\097\109\101"H.BackgroundTransparency=1 H.SizeConstraint=Enum.SizeConstraint.RelativeXX H.Position=UDim2.new(.5,0,1,0)H.Size=UDim2.new(u,0,u,0)H.Parent=I local i=createChatBubbleTail(A,UDim2.new(1,0,.5,0))i.Parent=H return I end function createChatImposter(F,u,A)local l=Instance.new("\073\109\097\103\101\076\097\098\101\108")l.Name="\068\105\097\108\111\103\080\108\097\099\101\104\111\108\100\101\114"l.Image="\114\098\120\097\115\115\101\116\058\047\047\116\101\120\116\117\114\101\115\047"..(tostring(F).."\046\112\110\103")l.BackgroundTransparency=1 l.BorderSizePixel=0 l.Position=UDim2.new(0,0,-1.25,0)l.Size=UDim2.new(1,0,1,0)local I=Instance.new("\073\109\097\103\101\076\097\098\101\108")I.Name="\068\111\116\068\111\116\068\111\116"I.Image="\114\098\120\097\115\115\101\116\058\047\047\116\101\120\116\117\114\101\115\047"..(tostring(u).."\046\112\110\103")I.BackgroundTransparency=1 I.BorderSizePixel=0 I.Position=UDim2.new(.001,0,A,0)I.Size=UDim2.new(1,0,.7,0)I.Parent=l return l end local o={}o.ChatBubble={}o.ChatBubbleWithTail={}o.ScalingChatBubbleWithTail={}o.CharacterSortedMsg=l()
--[[ FUNCTIONS ]]

local function lerpLength(msg, min, max)
	if FFlagUserChatNewMessageLengthCheck2 then
		return min + (max - min) * math.min(getMessageLength(msg) / 75.0, 1.0)
	else
		return min + (max - min) * math.min(string.len(msg) / 75.0, 1.0)
	end
end

local function createFifo()
	local this = {}
	this.data = {}

	local emptyEvent = Instance.new("BindableEvent")
	this.Emptied = emptyEvent.Event

	function this:Size()
		return #this.data
	end

	function this:Empty()
		return this:Size() <= 0
	end

	function this:PopFront()
		table.remove(this.data, 1)
		if this:Empty() then
			emptyEvent:Fire()
		end
	end

	function this:Front()
		return this.data[1]
	end

	function this:Get(index)
		return this.data[index]
	end

	function this:PushBack(value)
		table.insert(this.data, value)
	end

	function this:GetData()
		return this.data
	end

	return this
end

local function createCharacterChats()
	local this = {}

	this.Fifo = createFifo()
	this.BillboardGui = nil

	return this
end

local function createMap()
	local this = {}
	this.data = {}
	local count = 0

	function this:Size()
		return count
	end

	function this:Erase(key)
		if this.data[key] then
			count = count - 1
		end
		this.data[key] = nil
	end

	function this:Set(key, value)
		this.data[key] = value
		if value then
			count = count + 1
		end
	end

	function this:Get(key)
		if not key then
			return
		end
		if not this.data[key] then
			this.data[key] = createCharacterChats()
			local emptiedCon = nil
			emptiedCon = this.data[key].Fifo.Emptied:connect(function()
				emptiedCon:disconnect()
				this:Erase(key)
			end)
		end
		return this.data[key]
	end

	function this:GetData()
		return this.data
	end

	return this
end

local function createChatLine(message, bubbleColor, isLocalPlayer)
	local this = {}

	function this:ComputeBubbleLifetime(msg, isSelf)
		if isSelf then
			return lerpLength(msg, 8, 15)
		else
			return lerpLength(msg, 12, 20)
		end
	end

	this.Origin = nil
	this.RenderBubble = nil
	this.Message = message
	this.BubbleDieDelay = this:ComputeBubbleLifetime(message, isLocalPlayer)
	this.BubbleColor = bubbleColor
	this.IsLocalPlayer = isLocalPlayer

	return this
end

local function createPlayerChatLine(player, message, isLocalPlayer)
	local this = createChatLine(message, BubbleColor.WHITE, isLocalPlayer)

	if player then
		this.User = player.Name
		this.Origin = player.Character
	end

	return this
end

local function createGameChatLine(origin, message, isLocalPlayer, bubbleColor)
	local this = createChatLine(message, bubbleColor, isLocalPlayer)
	this.Origin = origin

	return this
end

function createChatBubbleMain(filePrefix, sliceRect)
	local chatBubbleMain = Instance.new("ImageLabel")
	chatBubbleMain.Name = "ChatBubble"
	chatBubbleMain.ScaleType = Enum.ScaleType.Slice
	chatBubbleMain.SliceCenter = sliceRect
	chatBubbleMain.Image = "rbxasset://textures/" .. tostring(filePrefix) .. ".png"
	chatBubbleMain.BackgroundTransparency = 1
	chatBubbleMain.BorderSizePixel = 0
	chatBubbleMain.Size = UDim2.new(1.0, 0, 1.0, 0)
	chatBubbleMain.Position = UDim2.new(0, 0, 0, 0)

	return chatBubbleMain
end

function createChatBubbleTail(position, size)
	local chatBubbleTail = Instance.new("ImageLabel")
	chatBubbleTail.Name = "ChatBubbleTail"
	chatBubbleTail.Image = "rbxasset://textures/ui/dialog_tail.png"
	chatBubbleTail.BackgroundTransparency = 1
	chatBubbleTail.BorderSizePixel = 0
	chatBubbleTail.Position = position
	chatBubbleTail.Size = size

	return chatBubbleTail
end

function createChatBubbleWithTail(filePrefix, position, size, sliceRect)
	local chatBubbleMain = createChatBubbleMain(filePrefix, sliceRect)

	local chatBubbleTail = createChatBubbleTail(position, size)
	chatBubbleTail.Parent = chatBubbleMain

	return chatBubbleMain
end

function createScaledChatBubbleWithTail(filePrefix, frameScaleSize, position, sliceRect)
	local chatBubbleMain = createChatBubbleMain(filePrefix, sliceRect)

	local frame = Instance.new("Frame")
	frame.Name = "ChatBubbleTailFrame"
	frame.BackgroundTransparency = 1
	frame.SizeConstraint = Enum.SizeConstraint.RelativeXX
	frame.Position = UDim2.new(0.5, 0, 1, 0)
	frame.Size = UDim2.new(frameScaleSize, 0, frameScaleSize, 0)
	frame.Parent = chatBubbleMain

	local chatBubbleTail = createChatBubbleTail(position, UDim2.new(1, 0, 0.5, 0))
	chatBubbleTail.Parent = frame

	return chatBubbleMain
end

function createChatImposter(filePrefix, dotDotDot, yOffset)
	local result = Instance.new("ImageLabel")
	result.Name = "DialogPlaceholder"
	result.Image = "rbxasset://textures/" .. tostring(filePrefix) .. ".png"
	result.BackgroundTransparency = 1
	result.BorderSizePixel = 0
	result.Position = UDim2.new(0, 0, -1.25, 0)
	result.Size = UDim2.new(1, 0, 1, 0)

	local image = Instance.new("ImageLabel")
	image.Name = "DotDotDot"
	image.Image = "rbxasset://textures/" .. tostring(dotDotDot) .. ".png"
	image.BackgroundTransparency = 1
	image.BorderSizePixel = 0
	image.Position = UDim2.new(0.001, 0, yOffset, 0)
	image.Size = UDim2.new(1, 0, 0.7, 0)
	image.Parent = result

	return result
end

local this = {}
this.ChatBubble = {}
this.ChatBubbleWithTail = {}
this.ScalingChatBubbleWithTail = {}
this.CharacterSortedMsg = createMap()
