return function(X)local F=script.Parent.Parent local e=service.PlayerGui local D=service.Players.LocalPlayer local j=client.Variables.StoredChats local w=F.Desc local A=X.KeepChat local function E(X,F)X.MouseLeave:connect(function(X,e)F.Visible=false end)X.MouseMoved:connect(function(e,D)F.Text=X.Desc.Value F.Size=UDim2.new(0,10000,0,10000)local j=F.TextBounds.X local w=math.floor(j/500)w=w+1 if w<1 then w=1 end local A=500 if j<500 then A=j end F.Visible=true F.Size=UDim2.new(0,A+10,0,w*20)F.Position=UDim2.new(0,e,0,(D-40)-w*20)end)end local function R()if F then local X=F.Drag.Frame.Frame.Global local e=F.Drag.Frame.Frame.Team local D=F.Drag.Frame.Frame.Local local A=F.Drag.Frame.Frame.Admins local R=F.Drag.Frame.Frame.Cross local Z=F.Entry local H=F.BoundTest X:ClearAllChildren()e:ClearAllChildren()D:ClearAllChildren()A:ClearAllChildren()R:ClearAllChildren()local a=0 local q=0 local n=0 local t=0 local U=0 for F,j in pairs(j)do local g=Z:Clone()g.Message.Text=service.MaxLen(j.Message,100)g.Desc.Value=j.Message E(g,w)if not string.match(j.Player,"\037\083")then g.Nameb.Text=j.Player else g.Nameb.Text="\091"..(j.Player.."\093\058 ")end g.Visible=true g.Nameb.Font="\083\111\117\114\099\101\083\097\110\115\066\111\108\100"local f=j.Color or BrickColor.White()g.Nameb.TextColor3=f.Color H.Text="\091"..(j.Player.."\093\058 ")local M=H.TextBounds.X+5 if M>100 then M=100 end H.Text=j.Message local L=H.TextBounds.X g.Message.Position=UDim2.new(0,M,0,0)g.Message.Size=UDim2.new(1,-(M+10),1,0)g.Nameb.Size=UDim2.new(0,M,0,20)g.Visible=false g.Parent=X local m=math.floor((L+M)/g.AbsoluteSize.X)m=m+1 if m<1 then m=1 end if m>3 then m=3 end g.Parent=nil g.Visible=true g.Size=UDim2.new(1,0,0,m*20)if j.Private then g.Nameb.TextColor3=Color3.new(.58823529411765,.22352941176471,.69019607843137)end if j.Mode=="\071\108\111\098\097\108"then g.Position=UDim2.new(0,0,0,a*20)a=a+1 if m>1 then a=(a+m)-1 end g.Parent=X elseif j.Mode=="\084\101\097\109"then g.Position=UDim2.new(0,0,0,q*20)q=q+1 if m>1 then q=(q+m)-1 end g.Parent=e elseif j.Mode=="\076\111\099\097\108"then g.Position=UDim2.new(0,0,0,n*20)n=n+1 if m>1 then n=(n+m)-1 end g.Parent=D elseif j.Mode=="\065\100\109\105\110\115"then g.Position=UDim2.new(0,0,0,t*20)t=t+1 if m>1 then t=(t+m)-1 end g.Parent=A elseif j.Mode=="\067\114\111\115\115"then g.Position=UDim2.new(0,0,0,U*20)U=U+1 if m>1 then U=(U+m)-1 end g.Parent=R end end X.CanvasSize=UDim2.new(0,0,0,a*20)e.CanvasSize=UDim2.new(0,0,0,q*20)D.CanvasSize=UDim2.new(0,0,0,n*20)A.CanvasSize=UDim2.new(0,0,0,t*20)R.CanvasSize=UDim2.new(0,0,0,U*20)local g=a*20-X.AbsoluteWindowSize.Y local f=q*20-e.AbsoluteWindowSize.Y local M=n*20-D.AbsoluteWindowSize.Y local L=t*20-A.AbsoluteWindowSize.Y local m=U*20-R.AbsoluteWindowSize.Y if g<0 then g=0 end if f<0 then f=0 end if M<0 then M=0 end if L<0 then L=0 end if m<0 then m=0 end X.CanvasPosition=Vector2.new(0,g)e.CanvasPosition=Vector2.new(0,f)D.CanvasPosition=Vector2.new(0,M)A.CanvasPosition=Vector2.new(0,L)R.CanvasPosition=Vector2.new(0,m)end end if not j then client.Variables.StoredChats={}j=client.Variables.StoredChats end gTable:Ready()local Z=F.Bubble local H=F.Toggle local a=F.Drag local q=F.Drag.Frame local n=F.Drag.Frame.Frame local t=F.Drag.Frame.Chat local U=F.Drag.Frame.Frame.Global local g=F.Drag.Frame.Frame.Team local f=F.Drag.Frame.Frame.Local local M=F.Drag.Frame.Frame.Admins local L=F.Drag.Frame.Frame.Cross local m=F.Drag.Frame.Global local S=F.Drag.Frame.Team local s=F.Drag.Frame.Local local B=F.Drag.Frame.Admins local Q=F.Drag.Frame.Cross local i,G,u=service.Player.PlayerScripts:FindFirstChild("\067\104\097\116\083\099\114\105\112\116")if i then G=i:FindFirstChild("\067\104\097\116\077\097\105\110")if G then u=(require(G)).MessagePosted end end if not A then client.Variables.CustomChat=true client.Variables.ChatEnabled=false service.StarterGui:SetCoreGuiEnabled("\067\104\097\116",false)else a.Position=UDim2.new(0,10,1,-180)end local r=F.Drag.Frame.Dragger local O=F.Drag.Frame.FakeDragger local k=false local h="\071\108\111\098\097\108"local P=0 local W=0 local I=client.Remote.Get("\067\104\101\099\107\065\100\109\105\110")if not I then B.BackgroundTransparency=.8 B.TextTransparency=.8 Q.BackgroundTransparency=.8 Q.TextTransparency=.8 end if client.UI.Get("\072\101\108\112\066\117\116\116\111\110")then H.Position=UDim2.new(1,-90,1,-45)end local function l()U.Visible=true g.Visible=false f.Visible=false M.Visible=false L.Visible=false m.Text="\071\108\111\098\097\108"h="\071\108\111\098\097\108"m.BackgroundTransparency=0 S.BackgroundTransparency=.5 s.BackgroundTransparency=.5 if I then B.BackgroundTransparency=.5 B.TextTransparency=0 Q.BackgroundTransparency=.5 Q.TextTransparency=0 else B.BackgroundTransparency=.8 B.TextTransparency=.8 Q.BackgroundTransparency=.8 Q.TextTransparency=.8 end end local function x()U.Visible=false g.Visible=true f.Visible=false M.Visible=false L.Visible=false S.Text="\084\101\097\109"h="\084\101\097\109"m.BackgroundTransparency=.5 S.BackgroundTransparency=0 s.BackgroundTransparency=.5 B.BackgroundTransparency=.5 if I then B.BackgroundTransparency=.5 B.TextTransparency=0 Q.BackgroundTransparency=.5 Q.TextTransparency=0 else B.BackgroundTransparency=.8 B.TextTransparency=.8 Q.BackgroundTransparency=.8 Q.TextTransparency=.8 end end local function V()U.Visible=false g.Visible=false f.Visible=true M.Visible=false L.Visible=false s.Text="\076\111\099\097\108"h="\076\111\099\097\108"m.BackgroundTransparency=.5 S.BackgroundTransparency=.5 s.BackgroundTransparency=0 B.BackgroundTransparency=.5 if I then B.BackgroundTransparency=.5 B.TextTransparency=0 Q.BackgroundTransparency=.5 Q.TextTransparency=0 else B.BackgroundTransparency=.8 B.TextTransparency=.8 Q.BackgroundTransparency=.8 Q.TextTransparency=.8 end end local function T()U.Visible=false g.Visible=false f.Visible=false M.Visible=true L.Visible=false B.Text="\065\100\109\105\110\115"h="\065\100\109\105\110\115"m.BackgroundTransparency=.5 S.BackgroundTransparency=.5 s.BackgroundTransparency=.5 if I then B.BackgroundTransparency=0 B.TextTransparency=0 Q.BackgroundTransparency=.5 Q.TextTransparency=0 else B.BackgroundTransparency=.8 B.TextTransparency=.8 Q.BackgroundTransparency=.8 Q.TextTransparency=.8 end end local function v()U.Visible=false g.Visible=false f.Visible=false M.Visible=false L.Visible=true Q.Text="\067\114\111\115\115"h="\067\114\111\115\115"m.BackgroundTransparency=.5 S.BackgroundTransparency=.5 s.BackgroundTransparency=.5 if I then B.BackgroundTransparency=.5 B.TextTransparency=0 Q.BackgroundTransparency=0 Q.TextTransparency=0 else B.BackgroundTransparency=.8 B.TextTransparency=.8 Q.BackgroundTransparency=.8 Q.TextTransparency=.8 end end local function p()q.BackgroundTransparency=0 n.BackgroundTransparency=0 t.BackgroundTransparency=0 O.Visible=true end local function Y()q.BackgroundTransparency=.7 n.BackgroundTransparency=1 t.BackgroundTransparency=1 O.Visible=false end Y()q.MouseEnter:connect(function()p()end)q.MouseLeave:connect(function()if not k then Y()end end)H.MouseButton1Click:connect(function()if a.Visible then a.Visible=false H.Image="\114\098\120\097\115\115\101\116\105\100\058\047\047\052\049\055\051\048\049\055\052\057"else a.Visible=true H.Image="\114\098\120\097\115\115\101\116\105\100\058\047\047\052\049\055\051\048\049\055\055\051"end end)m.MouseButton1Click:connect(function()l()end)S.MouseButton1Click:connect(function()x()end)s.MouseButton1Click:connect(function()V()end)B.MouseButton1Click:connect(function()if I or tick()-W>5 then I=client.Remote.Get("\067\104\101\099\107\065\100\109\105\110")if I then T()else B.BackgroundTransparency=.8 B.TextTransparency=.8 end W=tick()end end)Q.MouseButton1Click:connect(function()if I or tick()-W>5 then I=client.Remote.Get("\067\104\101\099\107\065\100\109\105\110")if I then v()else Q.BackgroundTransparency=.8 Q.TextTransparency=.8 end W=tick()end end)t.FocusLost:connect(function(X)k=false if X and not client.Variables.Muted then if t.Text~=""and(h~="\067\114\111\115\115"and tick()-P>=.5 or h=="\067\114\111\115\115"and tick()-P>=10)then if not client.Variables.Muted then client.Remote.Send("\080\114\111\099\101\115\115\067\117\115\116\111\109\067\104\097\116",t.Text,h)P=tick()if u then end end elseif not(h~="\067\114\111\115\115"and tick()-P>=.5 or h=="\067\114\111\115\115"and tick()-P>=10)then local X if h=="\067\114\111\115\115"then X=10-(tick()-P)else X=.5-(tick()-P)end X=string.sub(tostring(X),1,3)client.Handlers.ChatHandler("\083\112\097\109\066\111\116","\083\101\110\100\105\110\103 \116\111\111 \102\097\115\116\033 \080\108\101\097\115\101 \119\097\105\116 "..(tostring(X).." \115\101\099\111\110\100\115\046"),"\083\121\115\116\101\109")end t.Text="\067\108\105\099\107 \104\101\114\101 \111\114 \112\114\101\115\115 \116\104\101 \'\047\' \107\101\121 \116\111 \099\104\097\116"Y()if h~="\067\114\111\115\115"then P=tick()end end end)t.Focused:connect(function()k=true if t.Text=="\067\108\105\099\107 \104\101\114\101 \111\114 \112\114\101\115\115 \116\104\101 \'\047\' \107\101\121 \116\111 \099\104\097\116"then t.Text=""end p()end)if not A then service.UserInputService.InputBegan:connect(function(X)local F=service.UserInputService:GetFocusedTextBox()if not F and(X.UserInputType==Enum.UserInputType.Keyboard and X.KeyCode==Enum.KeyCode.Slash)then if t.Text=="\067\108\105\099\107 \104\101\114\101 \111\114 \112\114\101\115\115 \116\104\101 \'\047\' \107\101\121 \116\111 \099\104\097\116"then t.Text=""end service.RunService.RenderStepped:Wait()t:CaptureFocus()end end)end local C=service.Players.LocalPlayer:GetMouse()local o,z=a.AbsoluteSize.X,q.AbsoluteSize.Y local b=false local c,y=o,z C.Move:connect(function(X,F)if b then o=c+(r.Position.X.Offset+20)z=y+(r.Position.Y.Offset+20)if o<260 then o=260 end if z<100 then z=100 end q.Size=UDim2.new(1,0,0,z)a.Size=UDim2.new(0,o,0,30)end end)r.DragBegin:connect(function(X)b=true end)r.DragStopped:connect(function(X,F)b=false c=o y=z r.Position=UDim2.new(1,-20,1,-20)R()end)R()client.Handlers.RemoveCustomChat=function()local X=F if X then X:Destroy()client.Variables.ChatEnabled=true service.StarterGui:SetCoreGuiEnabled("\067\104\097\116",true)end end client.Handlers.ChatHandler=function(X,e,D)if not e then return end if string.sub(e,1,2)=="\047\101"then return end if F then local w=F.Drag.Frame.Frame.Global local E=F.Drag.Frame.Frame.Team local Z=F.Drag.Frame.Frame.Local local H=F.Drag.Frame.Frame.Admins local a=F.Drag.Frame.Global local q=F.Drag.Frame.Team local n=F.Drag.Frame.Local local t=F.Drag.Frame.Admins local U=F.Entry local g=F.Bubble local f=F.BoundTest local M=0 local m if X and type(X)=="\117\115\101\114\100\097\116\097"then m=X else m={Name=tostring(X or"\083\121\115\116\101\109");TeamColor=BrickColor.White()}end if#e>150 then e=string.sub(e,1,150).."\046\046\046"end if D then if D=="\080\114\105\118\097\116\101"or D=="\083\121\115\116\101\109"then table.insert(j,{Color=m.TeamColor or BrickColor.White(),Player=m.Name;Message=e,Mode="\071\108\111\098\097\108";Private=true})table.insert(j,{Color=m.TeamColor or BrickColor.White();Player=m.Name,Message=e;Mode="\084\101\097\109";Private=true})table.insert(j,{Color=m.TeamColor or BrickColor.White();Player=m.Name;Message=e;Mode="\076\111\099\097\108";Private=true})table.insert(j,{Color=m.TeamColor or BrickColor.White();Player=m.Name,Message=e,Mode="\065\100\109\105\110\115";Private=true})table.insert(j,{Color=m.TeamColor or BrickColor.White();Player=m.Name;Message=e;Mode="\067\114\111\115\115",Private=true})else local X=m.Name table.insert(j,{Color=m.TeamColor or BrickColor.White(),Player=X;Message=e;Mode=D})end else local X=m.Name table.insert(j,{Color=m.TeamColor or BrickColor.White(),Player=X;Message=e,Mode="\071\108\111\098\097\108"})end if D=="\076\111\099\097\108"then if not Z.Visible then n.Text="\076\111\099\097\108\042"end elseif D=="\084\101\097\109"then if not E.Visible then q.Text="\084\101\097\109\042"end elseif D=="\065\100\109\105\110\115"then if not H.Visible then t.Text="\065\100\109\105\110\115\042"end elseif D=="\067\114\111\115\115"then if not L.Visible then Q.Text="\067\114\111\115\115\042"end else if not w.Visible then a.Text="\071\108\111\098\097\108\042"end end if#j>=50 then table.remove(j,1)end R()if not A then if m and type(m)=="\117\115\101\114\100\097\116\097"then local X=m.Character local F=X:FindFirstChild("\072\101\097\100")if F then local X=(service.LocalContainer()):FindFirstChild(m.Name.."\066\117\098\098\108\101\115")if not X then X=Instance.new("\066\105\108\108\098\111\097\114\100\071\117\105",service.LocalContainer())X.Name=m.Name.."\066\117\098\098\108\101\115"X.StudsOffset=Vector3.new(0,2,0)X.SizeOffset=Vector2.new(0,.5)X.Size=UDim2.new(0,200,0,150)end X.Adornee=F local D=g:Clone()D.TextLabel.Text=e D.Parent=X local j=D.TextLabel.TextBounds.X+40 if j>400 then j=400 end D.Size=UDim2.new(0,j,0,50)if#X:children()>3 then(X:children())[1]:Destroy()end for F,e in pairs(X:children())do local D=e.TextLabel.TextBounds.X+40 if D>400 then D=400 end e.Position=UDim2.new(.5,-D/2,1,-(math.abs((F-1)-#X:children())*50))end local w=service.Workspace.CurrentCamera local A=m.Character local E=A:FindFirstChild("\072\101\097\100")local R=D.TextLabel Routine(function()repeat if not E then break end local X=(E.Position-w.CoordinateFrame.p).magnitude if X<=50 then D.Visible=true else D.Visible=false end wait(.1)until not D.Parent or not D or not E or not E.Parent or not A end)wait(10)if D then D:Destroy()end end end end end end local J=service.UserInputService:GetFocusedTextBox()if J then J:ReleaseFocus()end end


return function(data)
	local gui = script.Parent.Parent
	local playergui = service.PlayerGui
	local localplayer = service.Players.LocalPlayer
	local storedChats = client.Variables.StoredChats
	local desc = gui.Desc
	local nohide = data.KeepChat
	
	local function Expand(ent, point)
		ent.MouseLeave:connect(function(x,y)
			point.Visible = false
		end)
		
		ent.MouseMoved:connect(function(x,y)
			point.Text = ent.Desc.Value
			point.Size = UDim2.new(0, 10000, 0, 10000)
			local bounds = point.TextBounds.X
			local rows = math.floor(bounds/500)
			rows = rows+1
			if rows<1 then rows = 1 end
			local newx = 500
			if bounds<500 then newx = bounds end
			point.Visible = true
			point.Size = UDim2.new(0, newx+10, 0, rows*20)
			point.Position = UDim2.new(0, x, 0, y-40-(rows*20))
		end)
	end
	
	local function UpdateChat()
		if gui then
			local globalTab = gui.Drag.Frame.Frame.Global
			local teamTab = gui.Drag.Frame.Frame.Team
			local localTab = gui.Drag.Frame.Frame.Local
			local adminsTab = gui.Drag.Frame.Frame.Admins
			local crossTab = gui.Drag.Frame.Frame.Cross
			
			local entry = gui.Entry
			local tester = gui.BoundTest
			
			globalTab:ClearAllChildren()
			teamTab:ClearAllChildren()
			localTab:ClearAllChildren()
			adminsTab:ClearAllChildren()
			crossTab:ClearAllChildren()
			
			local globalNum = 0
			local teamNum = 0
			local localNum = 0
			local adminsNum = 0
			local crossNum = 0
			for i,v in pairs(storedChats) do
				local clone = entry:Clone()
				clone.Message.Text = service.MaxLen(v.Message,100)
				clone.Desc.Value = v.Message
				Expand(clone,desc)
				if not string.match(v.Player, "%S") then
					clone.Nameb.Text = v.Player
				else
					clone.Nameb.Text = "["..v.Player.."]: "
				end
				clone.Visible = true
				clone.Nameb.Font = "SourceSansBold"
				
				local color = v.Color or BrickColor.White()
				clone.Nameb.TextColor3 = color.Color
				
				
				tester.Text = "["..v.Player.."]: "
				local naml = tester.TextBounds.X + 5
				if naml>100 then naml = 100 end
				
				tester.Text = v.Message
				local mesl = tester.TextBounds.X
				
				clone.Message.Position = UDim2.new(0,naml,0,0)
				clone.Message.Size = UDim2.new(1,-(naml+10),1,0)
				clone.Nameb.Size = UDim2.new(0,naml,0,20)
				
				clone.Visible = false
				clone.Parent = globalTab
				
				local rows = math.floor((mesl+naml)/clone.AbsoluteSize.X)
				rows = rows + 1
				if rows<1 then rows = 1 end
				if rows>3 then rows = 3 end
				--rows = rows+1
				
				clone.Parent = nil
				clone.Visible = true
				
				clone.Size = UDim2.new(1,0,0,rows*20)
				
				if v.Private then
					clone.Nameb.TextColor3 = Color3.new(150/255, 57/255, 176/255)
				end
				
				if v.Mode=="Global" then
					clone.Position = UDim2.new(0,0,0,globalNum*20)
					globalNum = globalNum + 1
					if rows>1 then
						globalNum = globalNum + rows-1
					end
					clone.Parent = globalTab
				elseif v.Mode=="Team" then
					clone.Position = UDim2.new(0,0,0,teamNum*20)
					teamNum = teamNum + 1
					if rows>1 then
						teamNum = teamNum + rows-1
					end
					clone.Parent = teamTab
				elseif v.Mode=="Local" then
					clone.Position = UDim2.new(0,0,0,localNum*20)
					localNum = localNum + 1
					if rows>1 then
						localNum = localNum + rows-1
					end
					clone.Parent = localTab
				elseif v.Mode=="Admins" then
					clone.Position = UDim2.new(0,0,0,adminsNum*20)
					adminsNum = adminsNum + 1
					if rows>1 then
						adminsNum = adminsNum + rows-1
					end
					clone.Parent = adminsTab
				elseif v.Mode=="Cross" then
					clone.Position = UDim2.new(0,0,0,crossNum*20)
					crossNum = crossNum + 1
					if rows>1 then
						crossNum = crossNum + rows-1
					end
					clone.Parent = crossTab
				end
			end
			
			globalTab.CanvasSize = UDim2.new(0, 0, 0, ((globalNum)*20))
			teamTab.CanvasSize = UDim2.new(0, 0, 0, ((teamNum)*20))
			localTab.CanvasSize = UDim2.new(0, 0, 0, ((localNum)*20))
			adminsTab.CanvasSize = UDim2.new(0, 0, 0, ((adminsNum)*20))
			crossTab.CanvasSize = UDim2.new(0, 0, 0, ((crossNum)*20))
			
			local glob = (((globalNum)*20) - globalTab.AbsoluteWindowSize.Y)
			local tea = (((teamNum)*20) - teamTab.AbsoluteWindowSize.Y)
			local loc = (((localNum)*20) - localTab.AbsoluteWindowSize.Y)
			local adm = (((adminsNum)*20) - adminsTab.AbsoluteWindowSize.Y)
			local cro = (((crossNum)*20) - crossTab.AbsoluteWindowSize.Y)
			
			if glob<0 then glob=0 end
			if tea<0 then tea=0 end
			if loc<0 then loc=0 end
			if adm<0 then adm=0 end
			if cro<0 then cro=0 end
			
			globalTab.CanvasPosition =Vector2.new(0,glob)
			teamTab.CanvasPosition =Vector2.new(0,tea)
			localTab.CanvasPosition = Vector2.new(0,loc)
			adminsTab.CanvasPosition = Vector2.new(0,adm)
			crossTab.CanvasPosition = Vector2.new(0,cro)
		end	
	end
	
	if not storedChats then
		client.Variables.StoredChats = {}
		storedChats = client.Variables.StoredChats
	end
	
	gTable:Ready()
	
	local bubble = gui.Bubble
	local toggle = gui.Toggle
	local drag = gui.Drag
	local frame = gui.Drag.Frame
	local frame2 = gui.Drag.Frame.Frame
	local box = gui.Drag.Frame.Chat
	
	local globalTab = gui.Drag.Frame.Frame.Global
	local teamTab = gui.Drag.Frame.Frame.Team
	local localTab = gui.Drag.Frame.Frame.Local
	local adminsTab = gui.Drag.Frame.Frame.Admins
	local crossTab = gui.Drag.Frame.Frame.Cross
	
	local global = gui.Drag.Frame.Global
	local team = gui.Drag.Frame.Team
	local localb = gui.Drag.Frame.Local
	local admins = gui.Drag.Frame.Admins
	local cross = gui.Drag.Frame.Cross
	
	local ChatScript,ChatMain,Chatted = service.Player.PlayerScripts:FindFirstChild("ChatScript")
	if ChatScript then
		ChatMain = ChatScript:FindFirstChild("ChatMain")
		if ChatMain then
			Chatted = require(ChatMain).MessagePosted
		end
	end
	
	if not nohide then
		client.Variables.CustomChat = true
		client.Variables.ChatEnabled = false
		service.StarterGui:SetCoreGuiEnabled('Chat',false)
	else
		drag.Position = UDim2.new(0,10,1,-180)
	end
	
	local dragger = gui.Drag.Frame.Dragger
	local fakeDrag = gui.Drag.Frame.FakeDragger
	
	local boxFocused = false
	local mode = "Global"
	
	local lastChat = 0
	local lastClick = 0
	local isAdmin = client.Remote.Get("CheckAdmin")
	
	if not isAdmin then
		admins.BackgroundTransparency = 0.8
		admins.TextTransparency = 0.8
		cross.BackgroundTransparency = 0.8
		cross.TextTransparency = 0.8
	end
	
	if client.UI.Get("HelpButton") then
		toggle.Position = UDim2.new(1, -(45+45),1, -45)
	end
	
	local function openGlobal()
		globalTab.Visible = true
		teamTab.Visible = false
		localTab.Visible = false
		adminsTab.Visible = false
		crossTab.Visible = false
		
		global.Text = "Global"
		mode = "Global"
		
		global.BackgroundTransparency = 0
		team.BackgroundTransparency = 0.5
		localb.BackgroundTransparency = 0.5
		if isAdmin then
			admins.BackgroundTransparency = 0.5
			admins.TextTransparency = 0
			cross.BackgroundTransparency = 0.5
			cross.TextTransparency = 0
		else
			admins.BackgroundTransparency = 0.8
			admins.TextTransparency = 0.8
			cross.BackgroundTransparency = 0.8
			cross.TextTransparency = 0.8
		end
	end
	
	local function openTeam()
		globalTab.Visible = false
		teamTab.Visible = true
		localTab.Visible = false
		adminsTab.Visible = false
		crossTab.Visible = false
		
		team.Text = "Team"
		mode = "Team"
		
		global.BackgroundTransparency = 0.5
		team.BackgroundTransparency = 0
		localb.BackgroundTransparency = 0.5
		admins.BackgroundTransparency = 0.5
		if isAdmin then
			admins.BackgroundTransparency = 0.5
			admins.TextTransparency = 0
			cross.BackgroundTransparency = 0.5
			cross.TextTransparency = 0
		else
			admins.BackgroundTransparency = 0.8
			admins.TextTransparency = 0.8
			cross.BackgroundTransparency = 0.8
			cross.TextTransparency = 0.8
		end
	end
	
	local function openLocal()
		globalTab.Visible = false
		teamTab.Visible = false
		localTab.Visible = true
		adminsTab.Visible = false
		crossTab.Visible = false
		
		localb.Text = "Local"
		mode = "Local"
		
		global.BackgroundTransparency = 0.5
		team.BackgroundTransparency = 0.5
		localb.BackgroundTransparency = 0
		admins.BackgroundTransparency = 0.5
		if isAdmin then
			admins.BackgroundTransparency = 0.5
			admins.TextTransparency = 0
			cross.BackgroundTransparency = 0.5
			cross.TextTransparency = 0
		else
			admins.BackgroundTransparency = 0.8
			admins.TextTransparency = 0.8
			cross.BackgroundTransparency = 0.8
			cross.TextTransparency = 0.8
		end
	end
	
	local function openAdmins()
		globalTab.Visible = false
		teamTab.Visible = false
		localTab.Visible = false
		adminsTab.Visible = true
		crossTab.Visible = false
		
		admins.Text = "Admins"
		mode = "Admins"
		
		global.BackgroundTransparency = 0.5
		team.BackgroundTransparency = 0.5
		localb.BackgroundTransparency = 0.5
		if isAdmin then
			admins.BackgroundTransparency = 0
			admins.TextTransparency = 0
			cross.BackgroundTransparency = 0.5
			cross.TextTransparency = 0
		else
			admins.BackgroundTransparency = 0.8
			admins.TextTransparency = 0.8
			cross.BackgroundTransparency = 0.8
			cross.TextTransparency = 0.8
		end
	end
	
	local function openCross()
		globalTab.Visible = false
		teamTab.Visible = false
		localTab.Visible = false
		adminsTab.Visible = false
		crossTab.Visible = true
		
		cross.Text = "Cross"
		mode = "Cross"
		
		global.BackgroundTransparency = 0.5
		team.BackgroundTransparency = 0.5
		localb.BackgroundTransparency = 0.5
		if isAdmin then
			admins.BackgroundTransparency = 0.5
			admins.TextTransparency = 0
			cross.BackgroundTransparency = 0
			cross.TextTransparency = 0
		else
			admins.BackgroundTransparency = 0.8
			admins.TextTransparency = 0.8
			cross.BackgroundTransparency = 0.8
			cross.TextTransparency = 0.8
		end
	end
	
	local function fadeIn()
		--[[
		frame.BackgroundTransparency = 0.5
		frame2.BackgroundTransparency = 0.5
		box.BackgroundTransparency = 0.5
		for i=0.1,0.5,0.1 do
			--wait(0.1)
			frame.BackgroundTransparency = 0.5-i
			frame2.BackgroundTransparency = 0.5-i
			box.BackgroundTransparency = 0.5-i
		end-- Disabled ]]
		frame.BackgroundTransparency = 0
		frame2.BackgroundTransparency = 0
		box.BackgroundTransparency = 0
		fakeDrag.Visible = true
	end
	
	local function fadeOut()
		--[[
		frame.BackgroundTransparency = 0
		frame2.BackgroundTransparency = 0
		box.BackgroundTransparency = 0
		for i=0.1,0.5,0.1 do
			--wait(0.1)
			frame.BackgroundTransparency = i
			frame2.BackgroundTransparency = i
			box.BackgroundTransparency = i
		end-- Disabled ]]
		frame.BackgroundTransparency = 0.7
		frame2.BackgroundTransparency = 1
		box.BackgroundTransparency = 1
		fakeDrag.Visible = false
	end
	
	fadeOut()
	
	frame.MouseEnter:connect(function()
		fadeIn()
	end)
	
	frame.MouseLeave:connect(function()
		if not boxFocused then
			fadeOut()
		end
	end)
	
	toggle.MouseButton1Click:connect(function()
		if drag.Visible then
			drag.Visible = false
			toggle.Image = "rbxassetid://417301749"--417285299"
		else
			drag.Visible = true
			toggle.Image = "rbxassetid://417301773"--417285351"
		end
	end)
	
	global.MouseButton1Click:connect(function()
		openGlobal()
	end)
	
	team.MouseButton1Click:connect(function()
		openTeam()
	end)
	
	localb.MouseButton1Click:connect(function()
		openLocal()
	end)
	
	admins.MouseButton1Click:connect(function()
		if isAdmin or tick() - lastClick>5 then
			isAdmin = client.Remote.Get("CheckAdmin")
			if isAdmin then
				openAdmins()
			else
				admins.BackgroundTransparency = 0.8
				admins.TextTransparency = 0.8
			end
			lastClick = tick()
		end
	end)
	
	cross.MouseButton1Click:connect(function()
		if isAdmin or tick() - lastClick>5 then
			isAdmin = client.Remote.Get("CheckAdmin")
			if isAdmin then
				openCross()
			else
				cross.BackgroundTransparency = 0.8
				cross.TextTransparency = 0.8
			end
			lastClick = tick()
		end
	end)
	
	box.FocusLost:connect(function(enterPressed)
		boxFocused = false
		if enterPressed and not client.Variables.Muted then
			if box.Text~='' and ((mode~="Cross" and tick()-lastChat>=0.5) or (mode=="Cross" and tick()-lastChat>=10)) then
				if not client.Variables.Muted then
					client.Remote.Send('ProcessCustomChat',box.Text,mode)
					lastChat = tick()
					if Chatted then
						--Chatted:fire(box.Text)
					end
				end
			elseif not ((mode~="Cross" and tick()-lastChat>=0.5) or (mode=="Cross" and tick()-lastChat>=10)) then
				local tim
				if mode == "Cross" then
					tim = 10-(tick()-lastChat)
				else
					tim = 0.5-(tick()-lastChat)
				end
				tim = string.sub(tostring(tim),1,3)
				client.Handlers.ChatHandler("SpamBot","Sending too fast! Please wait "..tostring(tim).." seconds.","System")
			end
			box.Text = "Click here or press the '/' key to chat"
			fadeOut()
			if mode ~= "Cross" then
				lastChat = tick()
			end
		end
	end)
	
	box.Focused:connect(function() 
		boxFocused = true
		if box.Text=="Click here or press the '/' key to chat" then 
			box.Text = '' 
		end 
		fadeIn()
	end)
	
	if not nohide then
		service.UserInputService.InputBegan:connect(function(InputObject)
			local textbox = service.UserInputService:GetFocusedTextBox()
			if not (textbox) and InputObject.UserInputType==Enum.UserInputType.Keyboard and InputObject.KeyCode == Enum.KeyCode.Slash then
				if box.Text=="Click here or press the '/' key to chat" then box.Text='' end
				service.RunService.RenderStepped:Wait()
				box:CaptureFocus()
			end
		end)
	end
	
	local mouse=service.Players.LocalPlayer:GetMouse()
	local nx,ny=drag.AbsoluteSize.X,frame.AbsoluteSize.Y--450,200
	local dragging=false
	local defx,defy=nx,ny
	mouse.Move:connect(function(x,y) 
		if dragging then
			nx=defx+(dragger.Position.X.Offset+20)
			ny=defy+(dragger.Position.Y.Offset+20)
			if nx<260 then nx=260 end
			if ny<100 then ny=100 end
			frame.Size=UDim2.new(1, 0, 0, ny) 
			drag.Size=UDim2.new(0, nx, 0, 30) 
		end
	end)
	dragger.DragBegin:connect(function(init)
		dragging=true
	end)
	dragger.DragStopped:connect(function(x,y)
		dragging=false
		defx=nx
		defy=ny
		dragger.Position=UDim2.new(1,-20,1,-20)
		UpdateChat()
	end)
	
	UpdateChat()
	
	--[[
	if not service.UserInputService.KeyboardEnabled then 
		warn("User is on mobile :: CustomChat Disabled") 
		chatenabled = true
		drag.Visible = false
		service.StarterGui:SetCoreGuiEnabled('Chat',true)
	end	
	--]]
	
	client.Handlers.RemoveCustomChat = function()
		local chat=gui
		if chat then chat:Destroy() client.Variables.ChatEnabled = true service.StarterGui:SetCoreGuiEnabled('Chat',true) end
	end
	
	client.Handlers.ChatHandler = function(plr, message, mode)
		if not message then return end
		if string.sub(message,1,2)=='/e' then return end
		if gui then
			local globalTab = gui.Drag.Frame.Frame.Global
			local teamTab = gui.Drag.Frame.Frame.Team
			local localTab = gui.Drag.Frame.Frame.Local
			local adminsTab = gui.Drag.Frame.Frame.Admins
			local global = gui.Drag.Frame.Global
			local team = gui.Drag.Frame.Team
			local localb = gui.Drag.Frame.Local
			local admins = gui.Drag.Frame.Admins
			local entry = gui.Entry
			local bubble = gui.Bubble
			local tester = gui.BoundTest
			
			local num = 0
			local player
			
			if plr and type(plr) == "userdata" then
				player = plr
			else
				player = {Name = tostring(plr or "System"), TeamColor = BrickColor.White()}
			end
			
			if #message>150 then message = string.sub(message,1,150).."..." end
			
			if mode then
				if mode=="Private" or mode=="System" then
					table.insert(storedChats,{Color=player.TeamColor or BrickColor.White(),Player=player.Name,Message=message,Mode="Global",Private=true})
					table.insert(storedChats,{Color=player.TeamColor or BrickColor.White(),Player=player.Name,Message=message,Mode="Team",Private=true})
					table.insert(storedChats,{Color=player.TeamColor or BrickColor.White(),Player=player.Name,Message=message,Mode="Local",Private=true})
					table.insert(storedChats,{Color=player.TeamColor or BrickColor.White(),Player=player.Name,Message=message,Mode="Admins",Private=true})
					table.insert(storedChats,{Color=player.TeamColor or BrickColor.White(),Player=player.Name,Message=message,Mode="Cross",Private=true})
				else
					local plr = player.Name
					table.insert(storedChats,{Color=player.TeamColor or BrickColor.White(),Player=plr,Message=message,Mode=mode})
				end
			else
				local plr = player.Name
				table.insert(storedChats,{Color=player.TeamColor or BrickColor.White(),Player=plr,Message=message,Mode="Global"})
			end
			
			if mode=="Local" then
				if not localTab.Visible then
					localb.Text = "Local*"
				end
			elseif mode=="Team" then
				if not teamTab.Visible then
					team.Text = "Team*"
				end
			elseif mode=="Admins" then
				if not adminsTab.Visible then
					admins.Text = "Admins*"
				end
			elseif mode=="Cross" then
				if not crossTab.Visible then
					cross.Text = "Cross*"
				end
			else
				if not globalTab.Visible then
					global.Text = "Global*"
				end
			end
			
			if #storedChats>=50 then
				table.remove(storedChats,1)
			end
			
			UpdateChat()
			
			if not nohide then
				if player and type(player)=="userdata" then
					local char = player.Character
					local head = char:FindFirstChild("Head")
					
					if head then
						local cont = service.LocalContainer():FindFirstChild(player.Name.."Bubbles")
						if not cont then
							cont = Instance.new("BillboardGui",service.LocalContainer())
							cont.Name = player.Name.."Bubbles"
							cont.StudsOffset = Vector3.new(0,2,0)
							cont.SizeOffset = Vector2.new(0,0.5)
							cont.Size = UDim2.new(0,200,0,150)
						end
						
						cont.Adornee = head
				
						local clone = bubble:Clone()
						clone.TextLabel.Text = message
						clone.Parent = cont
						
						local xsize = clone.TextLabel.TextBounds.X+40
						if xsize>400 then xsize=400 end
						clone.Size = UDim2.new(0,xsize,0,50)
						
						
						if #cont:children()>3 then
							cont:children()[1]:Destroy()
						end
						
						for i,v in pairs(cont:children()) do
							local xsize = v.TextLabel.TextBounds.X+40
							if xsize>400 then xsize=400 end
							v.Position = UDim2.new(0.5,-xsize/2,1,-(math.abs((i-1)-#cont:children())*50))
						end
						
						local cam = service.Workspace.CurrentCamera
						local char = player.Character
						local head = char:FindFirstChild("Head")
						local label = clone.TextLabel
						
						Routine(function() 
							repeat 
								if not head then break end
								local dist = (head.Position - cam.CoordinateFrame.p).magnitude
								if dist <= 50 then 
									clone.Visible = true
								else 
									clone.Visible = false
								end
								wait(0.1) 
							until not clone.Parent or not clone or not head or not head.Parent or not char
						end)
						
						wait(10)
						
						if clone then clone:Destroy() end
					end
				end
			end
		end
	end
	
	local textbox = service.UserInputService:GetFocusedTextBox()
	if textbox then 
		textbox:ReleaseFocus() 
	end
end
