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