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

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()
