· 6 years ago · Mar 23, 2020, 08:04 AM
1local API = {}
2local UserInputService = game:GetService("UserInputService")
3local ReplicatedStorage = game:GetService("ReplicatedStorage")
4local Players = game:GetService("Players")
5local Verificator = require(script.Parent:WaitForChild("Verificator"))
6local ClientFunctions = require(script.Parent:WaitForChild("ClientFunctions"))
7local KeyBinder = require(script.Parent:WaitForChild("KeyBinder"))
8local AnimationController = require(script.Parent:WaitForChild("AnimationController"))
9local Assets = require(ReplicatedStorage.Databases.Assets)
10local Channels = require(ReplicatedStorage.Databases.Channels)
11local Teams = require(ReplicatedStorage.Databases.Teams)
12local MAX_MESSAGES = 20
13local HOLD_LENGTH = 0.5
14local player = Players.LocalPlayer
15local humanoid
16local messageEvent = ReplicatedStorage.Remotes.MessageEvent
17local channelConn = ReplicatedStorage.Remotes.ChannelConnect
18local radioFrame = ReplicatedStorage.UI.RadioFrame
19local messageFrame = ReplicatedStorage.UI.MessageFrame
20local allowedChannels = {}
21local channelCache = {}
22local unreadChannels = {}
23local currentRadio, currentChannel, unreadAccent, scrollFrame, headerFrame, headerLabel, sound, key
24local active = false
25local talk = false
26local alreadyChanged = false
27local function ParseMessageData(data)
28 for channel, channelTable in pairs(data) do
29 local channelFrame = scrollFrame[channel]
30 channelFrame:ClearAllChildren()
31 local canvasY = 0
32 local uiList = Instance.new("UIListLayout")
33 uiList.FillDirection = Enum.FillDirection.Vertical
34 uiList.SortOrder = Enum.SortOrder.LayoutOrder
35 uiList.VerticalAlignment = Enum.VerticalAlignment.Bottom
36 uiList.Padding = UDim.new(0, 2)
37 uiList.Parent = channelFrame
38 local modChannelNo = #channelTable % 2 == 0
39 local evenTrans = modChannelNo and 0.6 or 0.5
40 local oddTrans = modChannelNo and 0.5 or 0.6
41 for i = #channelTable, 1, -1 do
42 local messageData = channelTable[i]
43 local mFrame = messageFrame:Clone()
44 local formatUseName = string.format("%s:", messageData.SystemMessage and "SYSTEM" or messageData.Author)
45 local numNeededSpaces = ClientFunctions.GetNumberOfSpaces(formatUseName, Enum.Font.SourceSansBold, 18) + 1
46 local finalString = string.rep(" ", numNeededSpaces) .. messageData.Message
47 mFrame:WaitForChild("MessageLabel").Text = finalString
48 mFrame:WaitForChild("AuthorLabel").Text = formatUseName
49 mFrame.ImageTransparency = i % 2 == 0 and evenTrans or oddTrans
50 mFrame.ImageColor3 = messageData.SystemMessage and Assets.SystemMessageColor or Assets.BackgroundColor
51 local xSize = channelFrame.AbsoluteSize.X - 16
52 local mFrameSizeY = ClientFunctions.GetTextSize(finalString, 18, Enum.Font.SourceSans, Vector2.new(xSize, 1000)).y + 10
53 mFrame.Size = UDim2.new(1, 0, 0, mFrameSizeY)
54 canvasY = canvasY + mFrame.Size.Y.Offset + 2
55 mFrame.Parent = channelFrame
56 end
57 channelFrame.Size = UDim2.new(1, -14, 0, canvasY)
58 if channel == currentChannel then
59 sound:Play()
60 end
61 end
62 local currentFrameYSize = scrollFrame[currentChannel].Size.Y.Offset
63 scrollFrame.CanvasSize = UDim2.new(0, 0, 0, currentFrameYSize)
64 scrollFrame.CanvasPosition = Vector2.new(0, currentFrameYSize - scrollFrame.Size.Y.Offset)
65end
66local function Init(playerGui)
67 currentRadio = radioFrame:Clone()
68 local gui = Instance.new("ScreenGui")
69 gui.Name = "Radio"
70 gui.ResetOnSpawn = false
71 sound.Parent = gui
72 headerFrame = currentRadio:WaitForChild("HeaderFrame")
73 headerLabel = headerFrame:WaitForChild("HeaderLabel")
74 scrollFrame = currentRadio:WaitForChild("ScrollFrame")
75 unreadAccent = headerFrame:WaitForChild("ClipFrame"):WaitForChild("UnreadNot")
76 allowedChannels = API.GetRadioChannels()
77 local toNumber = {
78 [Enum.KeyCode.One] = 1,
79 [Enum.KeyCode.Two] = 2,
80 [Enum.KeyCode.Three] = 3,
81 [Enum.KeyCode.Four] = 4,
82 [Enum.KeyCode.Five] = 5,
83 [Enum.KeyCode.Six] = 6,
84 [Enum.KeyCode.Seven] = 7,
85 [Enum.KeyCode.Eight] = 8,
86 [Enum.KeyCode.Nine] = 9
87 }
88 UserInputService.InputBegan:Connect(function(inputObject, processed)
89 local num = toNumber[inputObject.KeyCode]
90 if not processed then
91 local tabPressed = UserInputService:IsKeyDown(Enum.KeyCode.Tab)
92 if tabPressed and num and allowedChannels[num] then
93 API.SetChannel(allowedChannels[num])
94 alreadyChanged = true
95 end
96 end
97 end)
98 talk = false
99 for _, v in pairs(allowedChannels) do
100 local clone = scrollFrame:WaitForChild("ScrollContainer"):Clone()
101 clone.Name = v
102 clone.Position = UDim2.new(1, 0, 0, 0)
103 clone.Parent = scrollFrame
104 end
105 scrollFrame.ScrollContainer:Destroy()
106 active = true
107 API.SetChannel(API.GetDefaultChannel() or allowedChannels[1])
108 currentRadio.Parent = gui
109 channelConn:FireServer(true)
110 local voiceDebounce = false
111 headerFrame.MouseButton1Click:Connect(function()
112 if not voiceDebounce then
113 voiceDebounce = true
114 API.ToggleTalkState()
115 wait(0.25)
116 voiceDebounce = false
117 end
118 end)
119 gui.Parent = playerGui
120end
121messageEvent.OnClientEvent:Connect(function(channel, messageData)
122 if not channelCache[channel] then
123 channelCache[channel] = {}
124 end
125 local thisCache = channelCache[channel]
126 if currentChannel ~= channel and unreadAccent then
127 unreadChannels[channel] = true
128 unreadAccent.Visible = true
129 end
130 table.insert(thisCache, 1, messageData)
131 if #thisCache > MAX_MESSAGES then
132 table.remove(thisCache, #thisCache)
133 end
134 ParseMessageData({
135 [channel] = thisCache
136 })
137end)
138player.Chatted:Connect(function(message)
139 local trimmed = message:gsub("^%s*(.-)%s*$", "%1")
140 if active and talk and trimmed ~= "" and trimmed:sub(1, 1) ~= "/" then
141 messageEvent:FireServer(currentChannel, trimmed)
142 end
143end)
144sound = Instance.new("Sound")
145sound.SoundId = Assets.RadioSound
146sound.Volume = 0.5
147local coreGui
148local conn
149function API.Init(argCoreGui, argHum)
150 coreGui = argCoreGui
151 humanoid = argHum
152end
153ReplicatedStorage.Remotes.radioRemote.OnClientEvent:Connect(function()
154 API.ShowRadio(coreGui, humanoid)
155end)
156function API.SetChannel(channelId)
157 if currentChannel and scrollFrame:FindFirstChild(currentChannel) then
158 scrollFrame[currentChannel].Position = UDim2.new(1, 0, 0, 0)
159 end
160 if unreadChannels[channelId] then
161 unreadChannels[channelId] = nil
162 local notAll = false
163 for _, v in pairs(unreadChannels) do
164 if v then
165 notAll = true
166 break
167 end
168 end
169 if not notAll then
170 unreadAccent.Visible = false
171 end
172 end
173 headerLabel.Text = Channels[channelId].Name:upper()
174 currentChannel = channelId
175 local currentFrame = scrollFrame[currentChannel]
176 currentFrame.Position = UDim2.new(0, 0, 0, 0)
177 scrollFrame.CanvasSize = UDim2.new(0, 0, 0, currentFrame.Size.Y.Offset)
178 scrollFrame.CanvasPosition = Vector2.new(0, currentFrame.Size.Y.Offset - scrollFrame.Size.Y.Offset)
179end
180function API.ShowRadio(gui, hum)
181 if not active and API.CanUseRadio() then
182 Init(gui)
183 do
184 local lastHold
185 local cancelNext = false
186 key = KeyBinder.KeyAction.new("SwitchC", "Use Radio", {
187 Enum.KeyCode.Tab
188 }, function(inputObject)
189 if inputObject.UserInputState == Enum.UserInputState.End then
190 lastHold = nil
191 if cancelNext or alreadyChanged then
192 cancelNext = false
193 return
194 end
195 API.ToggleTalkState()
196 elseif inputObject.UserInputState == Enum.UserInputState.Begin then
197 alreadyChanged = false
198 do
199 local thisTick = tick()
200 lastHold = thisTick
201 delay(HOLD_LENGTH, function()
202 if thisTick == lastHold and not alreadyChanged then
203 cancelNext = true
204 API.NextChannel()
205 end
206 end)
207 end
208 end
209 end)
210 local playerConn
211 playerConn = player.CharacterAdded:Connect(function()
212 playerConn:Disconnect()
213 API.HideRadio()
214 end)
215 hum.Died:Connect(function()
216 API.HideRadio()
217 end)
218 end
219 end
220end
221function API.HideRadio()
222 if active then
223 active = false
224 if key then
225 key:Remove()
226 end
227 currentRadio:Destroy()
228 channelConn:FireServer(false)
229 end
230end
231function API.CanUseRadio()
232 for i, _ in pairs(Channels) do
233 return true
234 end
235end
236function API.GetRadioChannels()
237 local channelReturn = {}
238 table.insert(channelReturn, "Global")
239 table.insert(channelReturn, "ES1")
240 table.insert(channelReturn, "LE1")
241 table.insert(channelReturn, "LE2")
242 table.sort(channelReturn, function(a, b)
243 return Channels[a].Priority < Channels[b].Priority
244 end)
245 return channelReturn
246end
247function API.GetDefaultChannel()
248 for _, v in pairs(Teams) do
249 if v.TeamColor == player.TeamColor then
250 return v.DefaultChannel
251 end
252 end
253end
254function API.UpdateTalk()
255 if active then
256 headerFrame.ImageColor3 = talk and Assets.PrimaryColor or Assets.BackgroundColor
257 end
258end
259local anim
260function API.ToggleTalkState()
261 if active then
262 talk = not talk
263 if humanoid then
264 if anim then
265 anim:Stop()
266 anim = nil
267 end
268 if talk then
269 anim = AnimationController.new(humanoid, Assets.RadioAnimation, nil, 2)
270 end
271 end
272 API.UpdateTalk()
273 end
274end
275function API.NextChannel()
276 if active then
277 local currentI
278 for i, v in pairs(allowedChannels) do
279 if currentChannel == v then
280 currentI = i
281 end
282 end
283 local newIndex = (currentI + 1) % #allowedChannels
284 newIndex = newIndex == 0 and #allowedChannels or newIndex
285 API.SetChannel(allowedChannels[newIndex])
286 end
287end
288return API