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


local function lerpLength(msg, min, max)
	return min + (max - min) * math.min(getMessageLength(msg) / 75.0, 1.0)
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()
