· 4 years ago · Mar 22, 2021, 12:24 PM
1--This module contains anything used by BOTH the SERVER and the CLIENT
2--That includes data libraries, global functions, etc
3
4print("Global API initialized")
5
6local Services = require(script.Parent.Services)
7
8local API = {}
9
10
11function API.NLerp(a, b, t) -- Lerping function for numbers
12 return a + (b - a) * t
13end
14
15function API.SolveIK(originCF, targetPos, l1, l2)
16 local localized = originCF:pointToObjectSpace(targetPos)
17 local localizedUnit = localized.unit
18 local l3 = localized.magnitude
19
20 local axis = Vector3.new(0, 0, -1):Cross(localizedUnit)
21 local angle = math.acos(-localizedUnit.Z)
22 local planeCF = originCF * CFrame.fromAxisAngle(axis, angle)
23
24 if l3 < math.max(l2, l1) - math.min(l2, l1) then
25 return planeCF * CFrame.new(0, 0, math.max(l2, l1) - math.min(l2, l1) - l3), -math.pi/2, math.pi
26 elseif l3 > l1 + l2 then
27 return planeCF, math.pi/2, 0
28 else
29 local a1 = -math.acos((-(l2 * l2) + (l1 * l1) + (l3 * l3)) / (2 * l1 * l3))
30 local a2 = math.acos(((l2 * l2) - (l1 * l1) + (l3 * l3)) / (2 * l2 * l3))
31 return planeCF, a1 + math.pi/2, a2 - a1
32 end
33end
34
35function API.RealWait(Framerate, Duration) -- Empty arguments meaning wait 1 frame
36 local Start = tick()
37 for i = 0,(Framerate*Duration) do -- Just setting a rough baseline for how long to wait
38 local CurrentTime = tick()
39 if CurrentTime-Start > Duration - (1/(Framerate+10)) then -- Adding a variance of 10 frames for accuracy
40 break -- Rather have it wait less than more
41 end
42 Services.RunService.Stepped:wait()
43 end
44end
45
46function API.Tween(Object, ...) -- (Duration 1, Style 2, Direction 3, Property 4, Value 5) Function making it easier to call the tweenservice
47 local Params = {...}
48 local TweenData = {}
49
50 local Property
51
52 for Index, Value in pairs(Params) do
53 if typeof(Value) ~= "table" then
54 if Index < 4 then
55 TweenData[#TweenData + 1] = Value
56 elseif Index == 5 then
57 Property = {Value = Params[6]}
58 break
59 end
60 else
61 Property = Value
62 break
63 end
64 end
65
66 if #TweenData ~= 0 then
67 if TweenData[2] and type(TweenData[2]) == "string" then
68 TweenData[2] = Enum.EasingStyle[TweenData[2]]
69 end
70 if TweenData[3] and type(TweenData[3]) == "string" then
71 TweenData[3] = Enum.EasingDirection[TweenData[3]]
72 end
73 end
74
75 local tw = Services.TweenService:Create(Object,TweenInfo.new(unpack(TweenData)),Property)
76 tw:Play()
77 return tw
78end
79
80function API.UniqueIdentifier(Base)
81 if not Base then Base = "" end
82 local str = Base..string.char(math.random(65,90))..string.char(math.random(65,90))
83 local num = tostring(math.floor(tick()*10000))
84 str = str..string.sub(num,-7,#num-1)..string.char(math.random(65,90))
85 return str
86end
87
88function API.CleanTable(Table)
89 if not Table then warn("Global API: No table") return end
90
91 for Key, Value in pairs(Table) do
92 if typeof(Value) == "table" then
93 API.CleanTable(Table[Key])
94 elseif typeof(Value) == "RBXScriptConnection" then
95 Table[Key]:Disconnect()
96 end
97
98 Table[Key] = nil
99 end
100
101 setmetatable(Table, {__mode = "kv"})
102
103 return Table
104end
105
106-- Parameters( source: instance[, range : number (99999) , deadPlayer : bool (false)] )
107-- Return an array: {Player = Character}
108function API.GetNearbyCharacters(source, range, deadPlayer)
109 if not source then warn("No source to get near by player") return end
110
111 local nearby = {}
112 local range = range or 99999
113
114 for _, player in pairs(Services.Players:GetPlayers()) do
115 local character = player.Character
116 if character then
117 local root = character:FindFirstChild("HumanoidRootPart")
118 local humanoid = character:FindFirstChild("Humanoid")
119
120 if root then
121 if not deadPlayer then
122 if humanoid.Health < 1 then
123 continue
124 end
125 end
126
127 local distance = API.GetDisplacement( root, source )
128 if distance and distance <= range then
129 nearby[player] = character
130 end
131 end
132 end
133 end
134
135 return nearby
136end
137
138function API.KeyUI(Frame, KeyCode)
139 if type(KeyCode) ~= "string" then KeyCode = KeyCode.Name end
140 local ButtonIcons = {
141 ["DPadLeft"] = "http://www.roblox.com/asset/?id=469343344",
142 ["DPadUp"] = "http://www.roblox.com/asset/?id=469343399",
143 ["DPadDown"] = "http://www.roblox.com/asset/?id=469343317",
144 ["DPadRight"] = "http://www.roblox.com/asset/?id=469343369",
145 ["ButtonA"] = "http://www.roblox.com/asset/?id=3904450911",
146 ["ButtonB"] = "http://www.roblox.com/asset/?id=3904450355",
147 ["ButtonX"] = "http://www.roblox.com/asset/?id=3904449753",
148 ["ButtonY"] = "http://www.roblox.com/asset/?id=3904449153",
149 ["ButtonR1"] = "http://www.roblox.com/asset/?id=3904451343",
150 ["ButtonL1"] = "http://www.roblox.com/asset/?id=3904451881",
151 ["ButtonR2"] = "http://www.roblox.com/asset/?id=3904452983",
152 ["ButtonL2"] = "http://www.roblox.com/asset/?id=3904453623",
153 ["ButtonL3"] = "http://www.roblox.com/asset/?id=3904454131",
154 ["ButtonR3"] = "http://www.roblox.com/asset/?id=3904454131",
155 ["MouseButton2"] = "http://www.roblox.com/asset/?id=3902201046",
156 ["MouseButton1"] = "http://www.roblox.com/asset/?id=3902200017",
157 }
158 local Abr = {
159 LeftShift = "LSft",
160 RightShift = "RSft",
161 Return = "Ent",
162 Backspace = "Bck",
163 Clear = "Clr",
164 Pause = "Pse",
165 Escape = "Esc",
166 Space = "___",
167 Quote = "'",
168 Minus = "-",
169 Period = ".",
170 Comma = ",",
171 Slash = "/",
172 One = "1",
173 Two = "2",
174 Three = "3",
175 Four = "4",
176 Five = "5",
177 Six = "6",
178 Seven = "7",
179 Eight = "8",
180 Nine = "9",
181 Zero = "0",
182 Semicolon = ";",
183 Equals = "=",
184 LeftBracket = "[",
185 RightBracket = "]",
186 Backslash = "\\",
187 Backquote = "`",
188 Delete = "Del",
189 Down = "Dwn",
190 Right = "->",
191 Left = "<-",
192 Insert = "Ins",
193 PageUp = "PgUp",
194 PageDown = "PgDn",
195 LeftAlt = "Alt",
196 RightAlt = "RAlt",
197 LeftControl = "Ctrl",
198 RightControl = "RCtrl",
199 CapsLock = "Caps",
200 NumLock = "Num",
201 ScrollLock = "Scr",
202 }
203 if ButtonIcons[KeyCode] ~= nil then
204 Frame.Key.Visible = false
205 Frame.Button.Visible = true
206 if KeyCode == "MouseButton1" or KeyCode == "MouseButton2" or KeyCode == "ButtonR3" or KeyCode == "ButtonL3" or string.find(KeyCode,"DPad",0,true) then
207 Frame.Button.BG.Visible = true
208 else
209 Frame.Button.BG.Visible = false
210 end
211 Frame.Button.Image = ButtonIcons[KeyCode]
212 else
213 Frame.Button.Visible = false
214 Frame.Key.Visible = true
215 if Abr[KeyCode] ~= nil then
216 Frame.Key.Size = UDim2.new(1.5,0,1,0)
217 Frame.Key.Code.Text = Abr[KeyCode]
218 else
219 Frame.Key.Size = UDim2.new(1,0,1,0)
220 Frame.Key.Code.Text = KeyCode
221 end
222 end
223end
224
225function API.GetDisplacement( a, b )
226 if a and b then
227 return (a.Position - b.Position).Magnitude
228 end
229
230 return
231end
232
233function API.Push( array, value )
234 array[#array + 1] = value
235 return array
236end
237
238-- Parameters( array, value[, shift : bool (true), valueIsIndex : bool (false)] )
239function API.Pop( array, value, shift, valueIsIndex )
240 local shift = shift or true
241
242 if valueIsIndex then
243 if shift then
244 table.remove(array, value)
245 else
246 array[value] = nil
247 end
248 else
249 local index = table.find(array, value)
250 if index then
251 if shift then
252 table.remove(array, index)
253 else
254 array[index] = nil
255 end
256 end
257 end
258
259 return array
260end
261
262return API -- The module will return EVERYTHING, but at least we can organize it into sub-tables first.