Advertisement
Kozenomenon

rbx fly util

Aug 14th, 2022 (edited)
1,200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 44.40 KB | Source Code | 0 0
  1. --[[
  2.     version 1.1.11
  3. ]]
  4. local version = "1.1.11"
  5. local RunService,Players,ContextActionService
  6.  
  7. local function makeTable(t)
  8.     local _t = (t and type(t)=="table" and t) or {}
  9.     setmetatable(_t,{})
  10.     return _t
  11. end
  12. -- will be returned at end
  13. local util = makeTable({
  14.     shutdownBegin = Instance.new("BindableEvent"),  -- when Fly Util shutdown starts, cannot call util funcs until done
  15.     shutdownEnd = Instance.new("BindableEvent"),    -- when Fly Util shutdown is finished, can now call util funcs
  16.     initBegin = Instance.new("BindableEvent"),      -- Fly Util starts initialization, cannot call util funcs until done
  17.     initEnd = Instance.new("BindableEvent"),        -- Fly Util init has finished, can now call util funcs
  18.     onFly = Instance.new("BindableEvent"),          -- when Flying is about to be turned on, Tool.Equipped was triggered
  19.     onUnFly = Instance.new("BindableEvent"),        -- when Flying is about to be turned off, Tool.Unequipped (or Tool.Equipped again) was triggered
  20.     onFire = Instance.new("BindableEvent"),         -- when Tool.Activated is triggered
  21.     errors = makeTable()
  22. })
  23. -- internal locals
  24. local LocalPlayer,Backpack,ControlModule,Character,Humanoid,BodyGyro,BodyVelocity,HumanoidRootPart,Animate,CurrentCamera,IdleAnim,MoveAnim,FlyBarGui,Bar
  25. local internal,_sched,_p,_init,_events,_threads = makeTable(),makeTable(),makeTable(),makeTable(),makeTable(),makeTable()
  26.  
  27. local MoveActions,flySettings = {},{}
  28.  
  29. local barSize,equippedToggle,unequippedTime,flyAmountLeft,flyTool,running,flyEnabled,jumpState,flyToolParent
  30.  
  31. --------
  32. do -----
  33. --------
  34.  
  35. --------
  36. end ----
  37. --------
  38.  
  39. --------
  40. do ----- Internal schedule / async tasks
  41. --------
  42. local _scheduled,_scheduledMax,_scheduleMinToProcess,_scheduleMaxToProcess,_schedulerRunning,_schedulerEvent,_schedulerEventFunc,_removeSchedulerEvent
  43. --- schedules the provided function (and calls it with any args after)
  44. function _sched:add(f, ...)
  45.     if not _sched:running() then return end
  46.     table.insert(_scheduled, {f, ...})
  47.     return #_scheduled
  48. end
  49. --- yields the current thread until the scheduler gives the ok
  50. function _sched:wait()
  51.     if not _sched:running() then return end
  52.     local thread = coroutine.running()
  53.     _sched:add(function()
  54.         coroutine.resume(thread)
  55.     end)
  56.     coroutine.yield()
  57. end
  58. function _sched:running()
  59.     if _schedulerRunning then
  60.         if not _scheduled or not _schedulerEvent or not _schedulerEventFunc or not _removeSchedulerEvent then
  61.             _sched:start()
  62.         end
  63.         return true
  64.     else
  65.         if _schedulerEvent then
  66.             _sched:stop()
  67.         end
  68.         return false
  69.     end
  70. end
  71. function _sched:start()
  72.     if _removeSchedulerEvent and type(_removeSchedulerEvent)=="function" then _removeSchedulerEvent() end
  73.     if _scheduled and type(_scheduled)=="table" then table.clear(_scheduled) end
  74.     _scheduled = nil
  75.     _scheduled = makeTable()
  76.     _scheduledMax = 1000
  77.     _scheduleMinToProcess = 8
  78.     _scheduleMaxToProcess = 15
  79.     _schedulerRunning = false
  80.     _schedulerEvent = nil
  81.     _schedulerEventFunc = nil
  82.     _schedulerEventFunc = function(deltaTimeOrDoAll)
  83.         if not _sched:running() then return end
  84.         local doAll = deltaTimeOrDoAll and type(deltaTimeOrDoAll)=="boolean"
  85.         if not doAll and #_scheduled > _scheduledMax then
  86.             table.remove(_scheduled, #_scheduled)
  87.         end
  88.         local cnt = 0
  89.         local min = type(_scheduleMinToProcess)=="number" and _scheduleMinToProcess or 8
  90.         local max = type(_scheduleMaxToProcess)=="number" and _scheduleMaxToProcess or 15
  91.         while #_scheduled>0 and doAll or cnt<math.random(min,max) do
  92.             if not _schedulerRunning then
  93.                 break
  94.             end
  95.             cnt=cnt+1
  96.             local currentf = _scheduled[1]
  97.             table.remove(_scheduled, 1)
  98.             if type(currentf) == "table" and type(currentf[1]) == "function" then
  99.                 pcall(unpack(currentf))
  100.             end
  101.         end
  102.     end
  103.     _removeSchedulerEvent = nil
  104.     _removeSchedulerEvent = function()
  105.         local removed
  106.         if _schedulerEvent then
  107.             local succ_rem,err_rem = pcall(function() _schedulerEvent:Disconnect();return; end)
  108.             removed = succ_rem
  109.         end
  110.         if not removed and RunService then
  111.             local hbConns = getconnections(RunService.Heartbeat)
  112.             for i,v in pairs(hbConns) do
  113.                 if v and v.Function == _schedulerEventFunc then
  114.                     _p:warn("scheduler - found connection when should have disconnected already!")
  115.                     local succ_rem,err_rem = pcall(function() v:Disconnect();return; end)
  116.                     if not succ_rem then
  117.                         pcall(function() v:Disable();return; end)
  118.                     end
  119.                     break
  120.                 end
  121.             end
  122.         end
  123.         _schedulerEvent = nil
  124.     end
  125.     _schedulerEvent = RunService.Heartbeat:Connect(_schedulerEventFunc)
  126.     _schedulerRunning = true
  127.     _p:debug("scheduler - started")
  128. end
  129. function _sched:stop(complete)
  130.     if _removeSchedulerEvent and type(_removeSchedulerEvent)=="function" then _removeSchedulerEvent() end
  131.     if complete and _schedulerEventFunc and type(_schedulerEventFunc)=="function" then
  132.         _schedulerEventFunc(true)
  133.     end
  134.     if _scheduled and type(_scheduled)=="table" then table.clear(_scheduled) end
  135.     _scheduled = nil
  136.     _scheduledMax = nil
  137.     _scheduleMinToProcess = nil
  138.     _scheduleMaxToProcess = nil
  139.     _schedulerRunning = nil
  140.     _schedulerEvent = nil
  141.     _schedulerEventFunc = nil
  142.     _removeSchedulerEvent = nil
  143.     _p:debug("scheduler - stopped. completed=",complete)
  144. end
  145. --------
  146. end ---- Internal schedule / async tasks
  147. --------
  148.  
  149. --------
  150. do ----- Thread Loop Control
  151. --------
  152. local __threads,__maxWait,__runThreadFunc,__killThread,__threadsRunning
  153. function _threads:running()
  154.     if __threadsRunning then
  155.         if  not __threads or type(__threads)~="table" or
  156.             not __maxWait or type(__maxWait)~="number" or
  157.             not __runThreadFunc or type(__runThreadFunc)~="function" or
  158.             not __killThread or type(__killThread)~="function" then
  159.             _threads:start(true)
  160.         end
  161.         return true
  162.     else
  163.         if __threads or __maxWait or __runThreadFunc or __killThread then
  164.             _threads:stop(true)
  165.         end
  166.         return false
  167.     end
  168. end
  169. function _threads:add(name,interval,func,await)
  170.     local me = self
  171.     local function __t_add()
  172.         if not name or type(name)~="string" or #name==0 then return end
  173.         if not func or type(func)~="function" then return end
  174.         if not me:running() then return end
  175.         local cadence = (interval and type(interval)=="number" and interval>0 and interval) or 0.1
  176.         _p:info("ThreadAdd - Begin. Name=",name," | Interval=",cadence," | Func=",func)
  177.         local th = __threads[name]
  178.         local co = th and th.co
  179.         local state = (co and coroutine.status(co)) or "nil"
  180.         if co and state~="dead" then
  181.             _p:warn("ThreadAdd - !Existing! Name=",name,"| Thread=",(co and tostring(co) or "nil")," | Status=",state,
  182.                                             " | runs=",(th and th.cnt~=nil and tostring(th.cnt) or "nil"),
  183.                                             " | Interval=",(th and th.interval~=nil and tostring(th.interval) or "nil"),
  184.                                             " | Func=",(th and th.func and tostring(th.func) or "nil"))
  185.             __killThread(name,th)
  186.         end
  187.         th = th or makeTable()
  188.         __threads[name] = th
  189.         th.kill = false
  190.         th.interval = cadence
  191.         th.func = func
  192.         th.cnt = 0
  193.         th.co = coroutine.create(__runThreadFunc)
  194.         coroutine.resume(th.co,name)
  195.         _p:info("ThreadAdd - Success. Name=",name," | Interval=",th.interval," | Func=",th.func," | Thread=",th.co," | Status=",coroutine.status(th.co))
  196.     end
  197.     if await then
  198.         __t_add()
  199.     else
  200.         coroutine.wrap(__t_add)()
  201.     end
  202. end
  203. function _threads:remove(name,await)
  204.     local function  __t_remove()
  205.         if not name or type(name)~="string" or #name==0 then return end
  206.         if not __threads or type(__threads)~="table" then return end
  207.         local th = __threads[name]
  208.         if th then
  209.             local co = th and th.co
  210.             local state = (co and coroutine.status(co)) or "nil"
  211.             _p:info("ThreadRemove - Begin. Name=",name,"| Thread=",(co and tostring(co) or "nil"),
  212.                                                       " | Status=",state,
  213.                                                       " | runs=",(th and th.cnt~=nil and tostring(th.cnt) or "nil"),
  214.                                                       " | Interval=",(th and th.interval~=nil and tostring(th.interval) or "nil"),
  215.                                                       " | Func=",(th and th.func and tostring(th.func) or "nil"))
  216.             if co and state~="dead" then
  217.                 __killThread(name,th)
  218.                 state = (co and coroutine.status(co)) or "nil"
  219.             end
  220.             _p:info("ThreadRemove - End. Name=",name,"| Thread=",(co and tostring(co) or "nil"),
  221.                                                     " | Status=",state,
  222.                                                     " | runs=",(th and th.cnt~=nil and tostring(th.cnt) or "nil"),
  223.                                                     " | Interval=",(th and th.interval~=nil and tostring(th.interval) or "nil"),
  224.                                                     " | Func=",(th and th.func and tostring(th.func) or "nil"))
  225.             th = nil
  226.             __threads[name] = nil
  227.         else
  228.             _p:info("ThreadRemove - Not Found. Name=",name)
  229.         end
  230.     end
  231.     if await then
  232.         __t_remove()
  233.     else
  234.         coroutine.wrap(__t_remove)()
  235.     end
  236. end
  237. function _threads:start(await)
  238.     local me = self
  239.     me:stop(await)
  240.     __threads = makeTable()
  241.     __maxWait = 2
  242.     __runThreadFunc = nil
  243.     __killThread = nil
  244.     __threadsRunning = false
  245.     __runThreadFunc = function(nm)
  246.         if not me:running() then return end
  247.         task.wait()
  248.         local th = __threads[nm]
  249.         _p:debug("Thread Begin - Name=",nm," | runs=",(th and th.cnt~=nil and tostring(th.cnt) or "nil"),
  250.                                            " | Interval=",(th and th.interval~=nil and tostring(th.interval) or "nil"),
  251.                                            " | Func=",(th and th.func and tostring(th.func) or "nil"),
  252.                                            " | Thread=",(th and th.co and tostring(th.co) or "nil"))
  253.         if th then th.cnt = 0 end
  254.         while th and not th.kill and th.func and task.wait(th.interval) do
  255.             th = __threads[nm]
  256.             if th and not th.kill and th.func then
  257.                 th.func()
  258.                 th.cnt = th.cnt + 1
  259.             end
  260.         end
  261.         _p:debug("Thread End - Name=",nm," | runs=",(th and th.cnt~=nil and tostring(th.cnt) or "nil"),
  262.                                          " | Interval=",(th and th.interval~=nil and tostring(th.interval) or "nil"),
  263.                                          " | Func=",(th and th.func and tostring(th.func) or "nil"),
  264.                                          " | Thread=",(th and th.co and tostring(th.co) or "nil"))
  265.     end
  266.     __killThread = function(nm,th)
  267.         if not nm or type(nm)~="string" or #nm==0 then return end
  268.         if not th or type(th)~="table" then return end
  269.         if not me:running() then return end
  270.         th.kill = true
  271.         local co = th and th.co
  272.         if not co or type(co)~="thread" then return end
  273.         local tm = tick()
  274.         local maxWait = type(__maxWait)=="number" and __maxWait or 2
  275.         while co and coroutine.status(co)~="dead" do
  276.             local nowTm = tick()
  277.             if nowTm-tm>maxWait then
  278.                 _p:warn("__killThread - !Killing! Name=",nm,"| Waited Time=",nowTm-tm," | Killing thread=",co)
  279.                 coroutine.close(co)
  280.                 _p:warn("__killThread - !Killed! Name=",nm,"| thread=",co," | status=",coroutine.status(co))
  281.             end
  282.             task.wait()
  283.         end
  284.         _p:debug("__killThread End. Name=",nm,"| Waited Time=",tick()-tm,"| thread=",(co and tostring(co) or "nil")," | status=",(co and coroutine.status(co) or "nil"))
  285.     end
  286.     __threadsRunning = true
  287. end
  288. function _threads:stop(await)
  289.     local me = self
  290.     local function __t_clear()
  291.         if __threads and type(__threads)=="table" then
  292.             _p:info("ThreadClear - Begin. Count=",#__threads)
  293.             for i,v in pairs(__threads) do
  294.                 _p:debug("ThreadClear - Name=",i," | th=",v)
  295.                 me:remove(i,true)
  296.             end
  297.             table.clear(__threads)
  298.             _p:info("ThreadClear - End. Count=",#__threads)
  299.         end
  300.         __threads = nil
  301.         __maxWait = nil
  302.         __runThreadFunc = nil
  303.         __killThread = nil
  304.         __threadsRunning = false
  305.     end
  306.     if await then
  307.         __t_clear()
  308.     else
  309.         coroutine.wrap(__t_clear)()
  310.     end
  311. end
  312. --------
  313. end ---- Thread Loop Control
  314. --------
  315.  
  316. --------
  317. do ----- Print Output
  318. --------
  319. local _rconsoleprint,_prnt_nl,_prnt_pref,_prnt,_rconsoleclear,_rconsolecreate,_rconsolename,_rconsolewarn,_rconsoleerr,_rconsoleinfo
  320. local _pColors = makeTable({
  321.     ["WHITE"]=Color3.fromRGB(255,255,255),
  322.     ["RED"]=Color3.fromRGB(255,0,0),
  323.     ["GREEN"]=Color3.fromRGB(0,255,0),
  324.     ["BLUE"]=Color3.fromRGB(0,0,255),
  325.     ["BLACK"]=Color3.fromRGB(0,0,0),
  326.     ["YELLOW"]=Color3.fromRGB(255,255,0),
  327.     ["CYAN"]=Color3.fromRGB(0,255,255),
  328.     ["MAGENTA"]=Color3.fromRGB(255,0,255)
  329. })
  330. ------
  331. do ---  print func setup (initial)
  332. ------
  333. _rconsoleprint = rconsoleprint or KRNL_LOADED and rconsoleinfo or output or printoutput or printdebug or print
  334. _prnt_nl = _rconsoleprint == rconsoleprint
  335. _prnt_pref = function(clr,msg)
  336.     if _prnt_nl and clr and msg then
  337.         _rconsoleprint("[")
  338.         if identifyexecutor and identifyexecutor()=="ScriptWare" then
  339.             _rconsoleprint(msg,clr:lower())
  340.         else
  341.             _rconsoleprint("@@"..clr.."@@")
  342.             _rconsoleprint(msg)
  343.             _rconsoleprint("@@LIGHT_GRAY@@")
  344.         end
  345.         _rconsoleprint("] ")
  346.     end
  347. end
  348. _prnt = function(msg: string, prefix: string, pref_color: string)
  349.     msg = msg or ""
  350.     if _prnt_nl then
  351.         if #msg==0 or msg:sub(#msg)~="\n" then msg = msg.."\n" end
  352.     end
  353.     if prefix and #prefix>0 then
  354.         if _prnt_nl and pref_color and #pref_color>0 then
  355.             _prnt_pref(pref_color,prefix)
  356.         else
  357.             msg = "["..prefix.."] "..msg
  358.         end
  359.     end
  360.     _rconsoleprint(msg)
  361. end
  362. _rconsoleclear = rconsoleclear or consoleclear or function() end
  363. _rconsolecreate = rconsolecreate or consolecreate or function() end
  364. _rconsolename = rconsolename or consolesettitle or rconsolesettitle or function() end
  365. _rconsolewarn = rconsolewarn or function(msg: string) _prnt(msg,"WARN","YELLOW") end
  366. _rconsoleerr = rconsoleerr or function(msg: string) _prnt(msg,"ERROR","RED") end
  367. _rconsoleinfo = rconsoleinfo or function(msg: string) _prnt(msg,"INFO","CYAN") end
  368. ------
  369. end --
  370. ------
  371. local function _trim(s)
  372.     return s:match'^()%s*$' and '' or s:match'^%s*(.*%S)'
  373. end
  374. local function _argsToPrintLine(...)
  375.     local args = {...}
  376.     local txt = ""
  377.     for i,v in ipairs(args) do
  378.         if #txt > 0 then
  379.             txt = _trim(txt) .. " | "
  380.         end
  381.         txt = txt .. tostring(v)
  382.     end
  383.     return txt
  384. end
  385.  
  386. local function _pcon(pargs,prefix,prefclr)
  387.     prefix = prefix or ""
  388.     if flySettings and flySettings.PrintUseExternalConsole and _prnt and type(_prnt)=="function" then
  389.         if _sched:running() then
  390.             _sched:add(_prnt,_argsToPrintLine(unpack(pargs)),prefix,prefclr)
  391.         else
  392.             _prnt(_argsToPrintLine(unpack(pargs)),prefix,prefclr)
  393.         end
  394.     --[[elseif printconsole then
  395.         local s = _argsToPrintLine((#prefix>0 and (prefix..":: ")) or "",unpack(pargs))
  396.         local c = _pColors[prefclr] or _pColors.WHITE
  397.         local r,g,b = c.R*255,c.G*255,c.B*255
  398.  
  399.         if _sched:running() then
  400.             _sched:add(printconsole,s,r,g,b)
  401.         else
  402.             printconsole(s,r,g,b)
  403.         end]]
  404.     else
  405.         if _sched:running() then
  406.             _sched:add(print,(#prefix>0 and (prefix..":: ")) or "",unpack(pargs))
  407.         else
  408.             print((#prefix>0 and (prefix..":: ")) or "",unpack(pargs))
  409.         end
  410.     end
  411. end
  412. function _p:error(...)
  413.     if flySettings and flySettings.PrintError then
  414.         _pcon({...},"FlyUtil::Error","RED")
  415.     end
  416. end
  417. function _p:warn(...)
  418.     if flySettings and flySettings.PrintWarn then
  419.         _pcon({...},"FlyUtil::Warn","YELLOW")
  420.     end
  421. end
  422. function _p:info(...)
  423.     if flySettings and flySettings.PrintInfo then
  424.         _pcon({...},"FlyUtil","WHITE")
  425.     end
  426. end
  427. function _p:debug(...)
  428.     if flySettings and flySettings.PrintDebug then
  429.         _pcon({...},"FlyUtil::Debug","MAGENTA")
  430.     end
  431. end
  432. function _p:clear()
  433.     if flySettings and flySettings.PrintUseExternalConsole and _rconsoleclear and type(_rconsoleclear)=="function" then
  434.         _rconsoleclear()
  435.     end
  436. end
  437. -------
  438. end ---- Print Output
  439. --------
  440.  
  441. --------
  442. do ----- Internal Init
  443. --------
  444. function _init:error(msg,stop)
  445.     _p:error("init_error! stop? ",stop," msg=",msg)
  446.     table.insert(util.errors,msg)
  447.     util.stop_init = (stop and true) or util.stop_init
  448. end
  449.  
  450. function _init:applySettings(newSettings)
  451.     if newSettings and type(newSettings)=="table" then
  452.         for i,v in pairs(newSettings) do
  453.             local old = flySettings[i]
  454.             if old~=v then
  455.                 flySettings[i] = v
  456.                 _p:debug("applySettings - ",i," - old=",old," | new=",v)
  457.             end
  458.         end
  459.     end
  460. end
  461.  
  462. function _init:loadResources(resources)
  463.     if resources then
  464.         if not IdleAnim then
  465.             local tmpIdleAsset; tmpIdleAsset = resources["IdleAnim"]
  466.             if tmpIdleAsset then
  467.                 IdleAnim = Humanoid:LoadAnimation(resources["IdleAnim"])
  468.                 _p:debug("loadResources - IdleAnim loaded.")
  469.             else
  470.                 _p:debug("loadResources - No IdleAnim.")
  471.             end
  472.         end
  473.         if not MoveAnim then
  474.             local tmpMoveAsset; tmpMoveAsset = resources["MoveAnim"]
  475.             if tmpMoveAsset then
  476.                 MoveAnim = Humanoid:LoadAnimation(resources["MoveAnim"])
  477.                 _p:debug("loadResources - MoveAnim loaded.")
  478.             else
  479.                 _p:debug("loadResources - No MoveAnim.")
  480.             end
  481.         end
  482.         if not FlyBarGui then
  483.             local tmpFlyBar; tmpFlyBar = resources["FlyBarGui"]
  484.             if tmpFlyBar and typeof(tmpFlyBar)=="Instance" and tmpFlyBar.ClassName=="ScreenGui" then
  485.                 local tmpBar = tmpFlyBar:WaitForChild("Bar")
  486.                 if tmpBar and typeof(tmpBar)=="Instance" and tmpBar.ClassName=="Frame" then
  487.                     FlyBarGui = tmpFlyBar
  488.                     Bar = tmpBar
  489.                     barSize = Bar.Size
  490.                     FlyBarGui.Parent = LocalPlayer.PlayerGui
  491.                     FlyBarGui.Enabled = true
  492.                     _p:debug("loadResources - FlyBarGui=",FlyBarGui," | Bar=",Bar," | barSize=",barSize)
  493.                 else
  494.                     _p:debug("loadResources - Nil/Invalid Bar=",tmpBar)
  495.                 end
  496.             else
  497.                 _p:debug("loadResources - Nil/Invalid FlyBarGui=",tmpFlyBar)
  498.             end
  499.         end
  500.     end
  501. end
  502.  
  503. function _init:validateParms(tool,settings,resources)
  504.     if not game or not game:IsLoaded() then
  505.         _init:error("Game not loaded yet!",true)
  506.         return false
  507.     else
  508.         RunService = (resources and resources["RunService"]) or game:FindService("RunService")
  509.         Players = (resources and resources["Players"]) or game:FindService("Players")
  510.         ContextActionService = (resources and resources["ContextActionService"]) or game:FindService("ContextActionService")
  511.     end
  512.     if not(RunService and Players and ContextActionService) then
  513.         _init:error("Services not available",true)
  514.         return false
  515.     end
  516.     if not tool or typeof(tool)~="Instance" or not tool:IsA("Tool") then
  517.         _init:error("Invalid or Nil Tool provided.",true)
  518.         return false
  519.     end
  520.     return true
  521. end
  522.  
  523. function _init:begin(toolParm,settingsParm,resourcesParm)
  524.  
  525.     LocalPlayer,Backpack,ControlModule,Character,Humanoid,BodyGyro,BodyVelocity,HumanoidRootPart,Animate,CurrentCamera,IdleAnim,MoveAnim,FlyBarGui,Bar =
  526.         nil,nil,nil,nil,nil,nil,nil,nil,nil,nil,nil,nil,nil,nil
  527.     MoveActions = nil
  528.     flySettings = nil
  529.     barSize,equippedToggle,unequippedTime,flyAmountLeft,flyTool,running,flyEnabled,jumpState,flyToolParent =
  530.         nil,nil,nil,nil,nil,nil,nil,nil,nil
  531.  
  532.     flySettings = makeTable({
  533.         MaxFlyTime = 1200,
  534.         FlySpeedMult = 2.5,
  535.         FlyDrainAmount = 1,
  536.         FlyRechargeAmount = 0.5,
  537.         FlyRechargeDelay = 1,
  538.         NoDisableAnimate = false,
  539.         ToolRemainsEquipped = false,
  540.         ToolUsesActivated = false,
  541.         ToolActivatedCooldown = 0,
  542.         HipHeightAdjustment = 0,
  543.         NoPlatformStand = false,
  544.         NoAnim = false,
  545.         NoGui = false,
  546.  
  547.         MaxWaitResources = 2,
  548.    
  549.         PrintUseExternalConsole = false,
  550.         PrintError = false,
  551.         PrintWarn = false,
  552.         PrintInfo = false,
  553.         PrintDebug = false
  554.     })
  555.     _init:applySettings(settingsParm)
  556.     _p:info("init - Applied Settings. ")
  557.  
  558.     _p:info("init - Begin")
  559.  
  560.     if _init:validateParms(toolParm,settingsParm,resourcesParm) then
  561.         _p:info("init - Initial Checks Passed")
  562.     else
  563.         _p:error("init - parm validation failure")
  564.         return
  565.     end
  566.  
  567.     local tool,resources
  568.     tool = toolParm
  569.     resources = resourcesParm
  570.    
  571.     _sched:start()
  572.     _p:info("init - scheduler started")
  573.  
  574.     LocalPlayer = Players.LocalPlayer
  575.     Backpack = LocalPlayer:WaitForChild("Backpack")
  576.     _p:debug("init - Backpack=",Backpack)
  577.     ControlModule = (resources and resources["ControlModule"]) or
  578.         require(LocalPlayer:WaitForChild("PlayerScripts"):WaitForChild("PlayerModule"):WaitForChild("ControlModule"));
  579.     _p:debug("init - ControlModule=",ControlModule)
  580.     Character = LocalPlayer.Character or LocalPlayer.CharacterAdded:Wait();
  581.     _p:debug("init - Character=",Character," | Parent=",Character.Parent)
  582.     while not Character.Parent do
  583.         _p:debug("init - Character.Parent is nil, waiting on ancestry changed...")
  584.         Character.AncestryChanged:Wait();
  585.     end
  586.     _p:debug("init - Character.Parent=",Character.Parent)
  587.     Humanoid = Character:WaitForChild("Humanoid")
  588.     _p:debug("init - Humanoid=",Humanoid)
  589.     HumanoidRootPart = Character:WaitForChild("HumanoidRootPart")
  590.     _p:debug("init - HumanoidRootPart=",HumanoidRootPart)
  591.     if flySettings.NoDisableAnimate then
  592.         Animate = nil
  593.     else
  594.         Animate = Character:WaitForChild("Animate")
  595.         _p:debug("init - Animate=",Animate)
  596.     end
  597.     CurrentCamera = workspace.CurrentCamera
  598.     _p:debug("init - CurrentCamera=",CurrentCamera)
  599.     if BodyGyro then
  600.         _p:warn("init - BodyGyro was not nil >>> ",BodyGyro)
  601.         pcall(function() BodyGyro.Parent=nil;BodyGyro:Destroy();return;end)
  602.     end
  603.     BodyGyro = Instance.new("BodyGyro");
  604.     BodyGyro.maxTorque = Vector3.new(1, 1, 1) * 1000000;
  605.     BodyGyro.P = 100000;
  606.     _p:debug("init - BodyGyro=",BodyGyro)
  607.     if BodyVelocity then
  608.         _p:warn("init - BodyVelocity was not nil >>> ",BodyVelocity)
  609.         pcall(function() BodyVelocity.Parent=nil;BodyVelocity:Destroy();return;end)
  610.     end
  611.     BodyVelocity = Instance.new("BodyVelocity");
  612.     BodyVelocity.maxForce = Vector3.new(1, 1, 1) * 1000000;
  613.     BodyVelocity.P = 10000;
  614.     _p:debug("init - BodyVelocity=",BodyVelocity)
  615.  
  616.     _p:info("init - Main Instances Complete")
  617.  
  618.     _init:loadResources(resources)
  619.     if  (not IdleAnim and not flySettings.NoAnim) or
  620.         (not MoveAnim and not flySettings.NoAnim) or
  621.         ((not FlyBarGui or not Bar) and not flySettings.NoGui) then
  622.         local startTm = tick()
  623.         _p:debug("init - waiting for resource load... StartTime=",startTm)
  624.         while (not IdleAnim or not MoveAnim or not FlyBarGui or not Bar) and startTm>(tick()-flySettings.MaxWaitResources) do
  625.             task.wait(0.1)
  626.             _p:debug("init - Attempt loadResources. IdleAnim=",IdleAnim," | MoveAnim=",MoveAnim," | FlyBarGui=",FlyBarGui," | Bar=",Bar," | TimeLeft=",startTm-(tick()-flySettings.MaxWaitResources))
  627.             _init:loadResources(resources)
  628.         end
  629.         _p:debug("init - loadResources success? ",(IdleAnim and MoveAnim and FlyBarGui and Bar)==true," | TimeTaken=",(tick()-startTm))
  630.     end
  631.  
  632.     flyTool = tool
  633.     flyToolParent = tool and tool.Parent
  634.     if not flyToolParent then
  635.         _p:warn("init - FlyTool has no Parent!")
  636.     else
  637.         _p:debug("init - FlyTool=",flyTool)
  638.         _p:debug("init - FlyTool.Parent=",flyToolParent)
  639.     end
  640.     flyAmountLeft = flySettings.MaxFlyTime
  641.  
  642.     MoveActions = makeTable({
  643.         forward = 0,
  644.         backward = 0,
  645.         right = 0,
  646.         left = 0,
  647.         up = 0
  648.     })
  649.  
  650.     running = true
  651.     flyEnabled = false;
  652.     equippedToggle = false;
  653.     unequippedTime = tick();
  654.     jumpState = false;
  655.  
  656.     ContextActionService:BindAction("forward", internal.HandleMoveAction, false, Enum.PlayerActions.CharacterForward);
  657.     ContextActionService:BindAction("backward", internal.HandleMoveAction, false, Enum.PlayerActions.CharacterBackward);
  658.     ContextActionService:BindAction("left", internal.HandleMoveAction, false, Enum.PlayerActions.CharacterLeft);
  659.     ContextActionService:BindAction("right", internal.HandleMoveAction, false, Enum.PlayerActions.CharacterRight);
  660.     ContextActionService:BindAction("up", internal.HandleMoveAction, false, Enum.PlayerActions.CharacterJump);
  661.     _p:debug("init - ContextActions Bound.")
  662.  
  663.     _threads:start(true)
  664.     _threads:add("MoveActions",0.1,function()
  665.         if not running then return end
  666.         local moveVec = ControlModule and ControlModule:GetMoveVector() or Humanoid and Humanoid.MoveDirection or Vector3:new(0,0,0);
  667.         MoveActions.forward = -moveVec.Z;
  668.         MoveActions.right = moveVec.X;
  669.     end)
  670.     _p:debug("init - MoveActions Loop Started.")
  671.  
  672.     _events:clear()
  673.     _events:add("CharacterRemovingEvent",LocalPlayer.CharacterRemoving,function()
  674.         _p:debug("CharacterRemovingEvent - Begin")
  675.         util:shutdown()
  676.         _p:debug("CharacterRemovingEvent - End")
  677.     end)
  678.     _p:debug("init - Connected CharacterRemovingEvent")
  679.  
  680.     _events:add("ToolParentChangeEvent",flyTool:GetPropertyChangedSignal("Parent"),function()
  681.         _p:debug("ToolParentChangeEvent - Old=",flyToolParent," | New=",flyTool.Parent)
  682.         flyToolParent = flyTool.Parent
  683.         if equippedToggle and flySettings.ToolRemainsEquipped and (not flyToolParent or flyToolParent~=Character) then
  684.             equippedToggle = false;
  685.             internal:UnFly();
  686.             unequippedTime = tick();
  687.         end
  688.         --[[
  689.         _p:debug("ToolParentChangeEvent - Begin")
  690.         if not flyTool.Parent then
  691.             if FlyBarGui then
  692.                 pcall(function()
  693.                     --FlyBarGui:Destroy()
  694.                     --_p:debug("ToolParentChangeEvent - FlyBarGui destroyed")
  695.                     FlyBarGui.Enabled = false
  696.                     _p:debug("ToolParentChangeEvent - FlyBarGui disabled")
  697.                 end)
  698.             end
  699.         end;
  700.         _p:debug("ToolParentChangeEvent - End")
  701.         ]]
  702.     end)
  703.     _p:debug("init - Connected ToolParentChangeEvent")
  704.  
  705.     local function removeConnections(signal,parent)
  706.         local cnt = 0
  707.         local otherToolConns = getconnections(signal)
  708.         _p:debug("init - removeConnections signal=",signal," | parent=",parent," | Cnt=",#otherToolConns)
  709.         for i, v in pairs(otherToolConns) do
  710.             _p:debug("init - removeConnections::",i," | Function=",(v and v.Function))
  711.             if v and v.Function then
  712.                
  713.                 local func = v.Function
  714.                 local succ_fi,fi = pcall(function()
  715.                     return debug.getinfo(func)
  716.                 end)
  717.                 if succ_fi then
  718.                     if flySettings.PrintDebug then
  719.                         _p:debug("init - removeConnections - got func info. Func=",func," | Cnt=",#fi)
  720.                         for i2,v2 in pairs(fi) do
  721.                             _p:debug(" --- FuncInfo::",i2,"=",v2," | type=",type(v2), " | typeof=",typeof(v2))
  722.                         end
  723.                     end
  724.                 else
  725.                     _p:error("init - removeConnections - failed getting func info! Func=",func," | Error=",fi)
  726.                 end
  727.                 local succ_fEnv,fEnv = pcall(function()
  728.                     local tmpfEnv = getfenv(func);
  729.                     _p:debug("init - removeConnections::",i," | script=",(tmpfEnv and tmpfEnv.script))
  730.                     if tmpfEnv and tmpfEnv.script and tmpfEnv.script.Parent==parent then
  731.                         sethiddenproperty(tmpfEnv.script,"Disabled",true)
  732.                         _p:debug("init - removeConnections::",i," | Disabled script=",(tmpfEnv and tmpfEnv.script)," | parent=",(tmpfEnv and tmpfEnv.script and tmpfEnv.script.Parent))
  733.                         cnt = cnt + 1
  734.                     else
  735.                         _p:debug("init - removeConnections::",i," | Ignoring script=",(tmpfEnv and tmpfEnv.script)," | parent=",(tmpfEnv and tmpfEnv.script and tmpfEnv.script.Parent))
  736.                         tmpfEnv = nil
  737.                     end
  738.                     return tmpfEnv
  739.                 end)
  740.                 if not succ_fEnv then
  741.                     _p:error("init - removeConnections - failed disabling other env! Func=",func," | Error=",fEnv)
  742.                 end
  743.                 if succ_fEnv and fEnv then
  744.                     _p:debug("init - removeConnections - disabling connection. Func=",func," | source=",fi.source)
  745.                     local succ_dis,err_dis = pcall(function()
  746.                         v:Disable();
  747.                         return
  748.                     end)
  749.                     if succ_dis then
  750.                         _p:debug("init - removeConnections - disabled connection. Func=",func," | source=",fi.source)
  751.                     else
  752.                         _p:error("init - removeConnections - failed to disable! Func=",func," | Error=",err_dis)
  753.                     end
  754.                 end
  755.             end
  756.         end
  757.         return cnt
  758.     end
  759.     local equippedCnt = removeConnections(flyTool.Equipped,flyTool)
  760.     local unequippedCnt = removeConnections(flyTool.Unequipped,flyTool)
  761.     local activatedCnt = removeConnections(flyTool.Activated,flyTool)
  762.     _p:debug("init - conns removed - Equipped=",equippedCnt," | Unequipped=",unequippedCnt," | Activated=",activatedCnt)
  763.     if equippedCnt==0 then
  764.         local toolScript = flyTool:FindFirstAncestorWhichIsA("LocalScript")
  765.         if toolScript then
  766.             _p:debug("init - removeConnections - alt method found tool script=",toolScript)
  767.             sethiddenproperty(toolScript,"Disabled",true)
  768.             _p:debug("init - removeConnections - Disabled script=",toolScript)
  769.         else
  770.             local toolChilds = flyTool:GetChildren()
  771.             for i,v in pairs(toolChilds) do
  772.                 _p:debug("init - removeConnections - child::",i,"=",v," type=",type(v)," typeof=",typeof(v)," class=",(v["ClassName"] or "nil"))
  773.                 if v and typeof(v)=="Instance" and v.ClassName=="LocalScript" then
  774.                     _p:debug("init - removeConnections - alt method2 found tool script=",v)
  775.                     sethiddenproperty(v,"Disabled",true)
  776.                     _p:debug("init - removeConnections - Disabled script=",v)
  777.                 end
  778.             end
  779.         end
  780.     end
  781.     _events:add("ToolEquippedEvent",flyTool.Equipped,flySettings.ToolRemainsEquipped and internal.ToolEquipped or internal.ToggleToolEquipped)
  782.     _p:debug("init - Connected "..(flySettings.ToolRemainsEquipped and "internal.ToolEquipped" or "internal.ToggleToolEquipped"))
  783.     if flySettings.ToolRemainsEquipped then
  784.         _events:add("ToolUnequippedEvent",flyTool.Unequipped,internal.ToolUnequipped)
  785.         _p:debug("init - Connected internal.ToolUnequipped")
  786.     end
  787.     if flySettings.ToolUsesActivated then
  788.         _events:add("ToolActivatedEvent",flyTool.Activated,internal.ToolActivated)
  789.         _p:debug("init - Connected internal.ToolActivated")
  790.     end
  791.  
  792.     _threads:add("FlyLoop",0.1,function()
  793.         if not running then return end
  794.         if equippedToggle then
  795.             flyAmountLeft = flyAmountLeft - flySettings.FlyDrainAmount;
  796.         elseif tick() - unequippedTime > flySettings.FlyRechargeDelay then
  797.             flyAmountLeft = flyAmountLeft + flySettings.FlyRechargeAmount;
  798.         end;
  799.         if flyAmountLeft > flySettings.MaxFlyTime then
  800.             flyAmountLeft = flySettings.MaxFlyTime;
  801.         elseif flyAmountLeft < 0 then
  802.             flyAmountLeft = 0;
  803.             equippedToggle = false;
  804.             internal:UnFly();
  805.             unequippedTime = tick();
  806.             flyTool.Parent = Backpack;
  807.         end;
  808.         if Bar then
  809.             Bar.Size = UDim2.new(flyAmountLeft / flySettings.MaxFlyTime * barSize.X.Scale, 0, barSize.Y.Scale, 0);
  810.         end
  811.     end)
  812.  
  813.     _p:info("init - End")
  814. end
  815. --------
  816. end ---- Internal Init
  817. --------
  818.  
  819. --------
  820. do ----- Internal Events Mgmt
  821. --------
  822. local __events
  823. function _events:remove(name)
  824.     if not name or type(name)~="string" or #name==0 then
  825.         return
  826.     end
  827.     __events = __events or makeTable()
  828.     local ev=__events[name]
  829.     if ev and typeof(ev)=="RBXScriptConnection" then
  830.         local succ_ev,err_ev = pcall(function() ev:Disconnect();return; end)
  831.         if succ_ev then
  832.             _p:debug("RemoveEvent - Success. Name=",name)
  833.         else
  834.             _p:error("RemoveEvent - Failed! Name=",name," | Error=",err_ev)
  835.         end
  836.     end
  837.     __events[name]=nil
  838. end
  839. function _events:add(name,signal,func)
  840.     if not name or type(name)~="string" or #name==0 or not signal or typeof(signal)~="RBXScriptSignal" or not func or type(func)~="function" then
  841.         return
  842.     end
  843.     __events = __events or makeTable()
  844.     _events:remove(name)
  845.     local succ_ev,err_ev = pcall(function() __events[name]=signal:Connect(func);return; end)
  846.     if succ_ev then
  847.         _p:debug("AddEvent - Success. Name=",name)
  848.     else
  849.         _p:error("AddEvent - Failed! Name=",name," | Error=",err_ev)
  850.     end
  851. end
  852. function _events:clear()
  853.     __events = __events or makeTable()
  854.     for i,v in pairs(__events) do
  855.         _events:remove(i)
  856.     end
  857.     table.clear(__events)
  858. end
  859. --------
  860. end ---- Internal Events Mgmt
  861. --------
  862.  
  863. --------
  864. do ----- Internal Fly Mechanics
  865. --------
  866. function internal:ToggleFlyPhysics(toggle)
  867.     _p:debug("ToggleFlyPhysics Begin - ",toggle)
  868.     flyEnabled = toggle;
  869.     if BodyGyro then
  870.         BodyGyro.Parent = flyEnabled and HumanoidRootPart or nil;
  871.         BodyGyro.CFrame = HumanoidRootPart.CFrame;
  872.     end
  873.     if BodyVelocity then
  874.         BodyVelocity.Parent = flyEnabled and HumanoidRootPart or nil;
  875.         BodyVelocity.Velocity = Vector3.new();
  876.     end
  877.     if Animate then
  878.         Animate.Disabled = flyEnabled;
  879.     end
  880.     _p:debug("ToggleFlyPhysics End - ",toggle)
  881. end
  882. function internal.HandleMoveAction(actionName, userInputState, inputObj)
  883.     --task.wait();
  884.     if userInputState == Enum.UserInputState.Begin then
  885.         MoveActions[actionName] = 1;
  886.         _p:debug("MoveAction Begin - ",actionName)
  887.     elseif userInputState == Enum.UserInputState.End then
  888.         MoveActions[actionName] = 0;
  889.         _p:debug("MoveAction End - ",actionName)
  890.     end;
  891.     if flyEnabled then
  892.  
  893.     end;
  894.     return Enum.ContextActionResult.Pass;
  895. end
  896. function internal.HumanoidStateChanged(oldState, newState)
  897.     _p:debug("HumanoidStateChanged Old=",oldState," | New=",newState)
  898.     if newState == Enum.HumanoidStateType.Landed then
  899.         jumpState = false;
  900.         return;
  901.     end;
  902.     if newState == Enum.HumanoidStateType.Jumping then
  903.         jumpState = true;
  904.     end;
  905. end
  906. function internal.FlyMoveMath()
  907.     if flyEnabled then
  908.         local cf = CurrentCamera.CFrame;
  909.         local v13 = cf.rightVector * (MoveActions.right - MoveActions.left) + cf.lookVector * (MoveActions.forward - MoveActions.backward)
  910.                     + cf.upVector * MoveActions.up
  911.         if v13:Dot(v13) > 0 then
  912.             v13 = v13.unit;
  913.         end;
  914.         BodyGyro.CFrame = cf;
  915.         BodyVelocity.Velocity = v13 * Humanoid.WalkSpeed * flySettings.FlySpeedMult;
  916.     end;
  917. end
  918. function internal:Fly()
  919.     if flyEnabled then return end
  920.     _p:debug("Fly Begin")
  921.     util.onFly:Fire()
  922.     if Humanoid then
  923.         if Humanoid:GetState() == Enum.HumanoidStateType.Dead then
  924.             _p:error("Fly - Dead! Cannot 'Fly'")
  925.             return
  926.         else
  927.             if not flySettings.NoPlatformStand then
  928.                 Humanoid.PlatformStand = true
  929.             end
  930.             local hipAdj = flySettings.HipHeightAdjustment
  931.             if hipAdj~=nil and type(hipAdj)=="number" then
  932.                 Humanoid.HipHeight = Humanoid.HipHeight + hipAdj
  933.             end
  934.         end
  935.     else
  936.         _p:error("Fly - No Humanoid!!! Cannot 'Fly'")
  937.         return
  938.     end
  939.     if MoveAnim then
  940.         local succ_play,err_play = pcall(function()
  941.             MoveAnim:Play()
  942.             _p:debug("Fly - MoveAnim played")
  943.             return
  944.         end)
  945.         if not succ_play then
  946.             _p:error("Fly - failed to play MoveAnim. Error=",err_play)
  947.         end
  948.     end
  949.     internal:ToggleFlyPhysics(true);
  950.     _events:add("HumanoidStateChanged",Humanoid.StateChanged,internal.HumanoidStateChanged)
  951.     _events:add("RenderStepped_FlyMoveMath",RunService.RenderStepped,internal.FlyMoveMath)
  952.     _p:debug("Fly End")
  953. end
  954. function internal:UnFly()
  955.     if not flyEnabled then return end
  956.     _p:debug("UnFly Begin")
  957.     util.onUnFly:Fire()
  958.     if MoveAnim then
  959.         local succ_stop,err_stop = pcall(function()
  960.             MoveAnim:Stop()
  961.             _p:debug("UnFly - MoveAnim stopped")
  962.             return
  963.         end)
  964.         if not succ_stop then
  965.             _p:error("UnFly - Failed to stop MoveAnim! Error=",err_stop)
  966.         end
  967.     end
  968.     if Humanoid then
  969.         local hipAdj = flySettings.HipHeightAdjustment
  970.         if hipAdj~=nil and type(hipAdj)=="number" then
  971.             Humanoid.HipHeight = Humanoid.HipHeight - hipAdj
  972.         end
  973.         if not flySettings.NoPlatformStand then
  974.             Humanoid.PlatformStand = false
  975.         end
  976.         task.delay(0.1, function()
  977.             Humanoid:ChangeState(Enum.HumanoidStateType.Freefall);
  978.             Humanoid:ChangeState(Enum.HumanoidStateType.Freefall);
  979.             Humanoid:ChangeState(Enum.HumanoidStateType.Freefall);
  980.             Humanoid:ChangeState(Enum.HumanoidStateType.Freefall);
  981.             _p:debug("UnFly - Humanoid State Changed >>> ",Humanoid:GetState())
  982.         end);
  983.     end
  984.     internal:ToggleFlyPhysics(false);
  985.     _events:remove("HumanoidStateChanged")
  986.     _events:remove("RenderStepped_FlyMoveMath")
  987.     _p:debug("UnFly End")
  988. end
  989. function internal.ToolEquipped()
  990.     coroutine.wrap(function()
  991.         if not running then return end
  992.         if not equippedToggle and flyAmountLeft > 0 then
  993.             internal:Fly();
  994.             equippedToggle = true;
  995.         else
  996.             task.wait(0.1);
  997.             if not running then return end
  998.             flyTool.Parent = Backpack
  999.         end
  1000.     end)()
  1001. end
  1002. function internal.ToolUnequipped()
  1003.     coroutine.wrap(function()
  1004.         if not running then return end
  1005.         if equippedToggle then
  1006.             internal:UnFly();
  1007.             equippedToggle = false;
  1008.             unequippedTime = tick();
  1009.         end
  1010.     end)()
  1011. end
  1012. function internal.ToggleToolEquipped()
  1013.     coroutine.wrap(function()
  1014.         if not running then return end
  1015.         if equippedToggle then
  1016.             internal:UnFly();
  1017.             equippedToggle = false;
  1018.             unequippedTime = tick();
  1019.         elseif not equippedToggle and flyAmountLeft > 0 then
  1020.             internal:Fly();
  1021.             equippedToggle = true;
  1022.         end;
  1023.         task.wait(0.1);
  1024.         if not running then return end
  1025.         flyTool.Parent = Backpack
  1026.     end)()
  1027. end
  1028. local lastActivated
  1029. function internal.ToolActivated()
  1030.     if not running or not equippedToggle then return end
  1031.     local nowTm = tick()
  1032.     local tmp = flySettings.ToolActivatedCooldown
  1033.     local activatedCooldown = tmp and type(tmp)=="function" and tmp() or tmp
  1034.     if not lastActivated or not activatedCooldown or nowTm-lastActivated>=activatedCooldown then
  1035.         util.onFire:Fire()
  1036.         lastActivated = nowTm
  1037.     end
  1038. end
  1039. --------
  1040. end ---- Internal Fly Mechanics
  1041. --------
  1042.  
  1043.  
  1044.  
  1045. local shutdownRunning=false
  1046. function util:shutdown()
  1047.     if shutdownRunning then return end
  1048.     shutdownRunning=true
  1049.     util.shutdownBegin:Fire()
  1050.     coroutine.wrap(function()
  1051.         task.wait()
  1052.         _p:info("shutdown - Begin")
  1053.         running = false
  1054.         internal:UnFly();
  1055.         equippedToggle = false;
  1056.         unequippedTime = tick();
  1057.         jumpState = false;
  1058.         _events:clear()
  1059.         _threads:stop(true)
  1060.         if ContextActionService then
  1061.             ContextActionService:UnbindAction("forward");
  1062.             ContextActionService:UnbindAction("backward");
  1063.             ContextActionService:UnbindAction("left");
  1064.             ContextActionService:UnbindAction("right");
  1065.             ContextActionService:UnbindAction("up");
  1066.             _p:debug("shutdown - ContextActions unbound.")
  1067.         end
  1068.         --[[
  1069.         if FlyBarGui then
  1070.             local succ_des,err_des = pcall(function()
  1071.                 --FlyBarGui:Destroy();
  1072.                 --_p:debug("shutdown - FlyBarGui destroyed")
  1073.                 FlyBarGui.Enabled = false
  1074.                 return
  1075.             end)
  1076.             if not succ_des then
  1077.                 _p:error("shutdown - failed to destroy FlyBarGui! Error=",err_des)
  1078.             end
  1079.         end
  1080.         ]]
  1081.         if BodyVelocity then
  1082.             local succ_des,err_des = pcall(function() BodyVelocity.Parent=nil;BodyVelocity:Destroy();return;end)
  1083.             if succ_des then
  1084.                 BodyVelocity=nil
  1085.                 _p:debug("shutdown - BodyVelocity destroyed")
  1086.             else
  1087.                 _p:error("shutdown - failed to destroy BodyVelocity! Error=",err_des)
  1088.             end
  1089.         end
  1090.         if BodyGyro then
  1091.             local succ_des,err_des = pcall(function() BodyGyro.Parent=nil;BodyGyro:Destroy();return;end)
  1092.             if succ_des then
  1093.                 BodyGyro=nil
  1094.                 _p:debug("shutown - BodyGyro destroyed")
  1095.             else
  1096.                 _p:error("shutdown - failed to destroy BodyGyro! Error=",err_des)
  1097.             end
  1098.         end
  1099.         task.wait(0.1)
  1100.         _sched:stop(true)
  1101.         _p:info("shutdown - Scheduler stopped")
  1102.         _p:info("shutdown - End")
  1103.         shutdownRunning=false
  1104.         LocalPlayer,Backpack,ControlModule,Character,Humanoid,BodyGyro,BodyVelocity,HumanoidRootPart,Animate,CurrentCamera,IdleAnim,MoveAnim,FlyBarGui,Bar =
  1105.             nil,nil,nil,nil,nil,nil,nil,nil,nil,nil,nil,nil,nil,nil
  1106.         MoveActions = nil
  1107.         barSize,equippedToggle,unequippedTime,flyAmountLeft,flyTool,running,flyEnabled,jumpState,flyToolParent =
  1108.             nil,nil,nil,nil,nil,nil,nil,nil,nil
  1109.         util.shutdownEnd:Fire()
  1110.     end)()
  1111. end
  1112.  
  1113. local initRunning=false
  1114. function util:init(toolParm,settingsParm,resourcesParm)
  1115.     if initRunning then return end
  1116.     initRunning=true
  1117.     util.initBegin:Fire()
  1118.     local tool,_settings,resources = toolParm,settingsParm,resourcesParm
  1119.     coroutine.wrap(function()
  1120.         task.wait()
  1121.         _init:begin(tool,_settings,resources)
  1122.         initRunning=false
  1123.         util.initEnd:Fire()
  1124.     end)()
  1125. end
  1126.  
  1127. function util:getStatus()
  1128.     if initRunning or shutdownRunning then
  1129.         return "wait"
  1130.     elseif running then
  1131.         return "not_running"
  1132.     else
  1133.         return "running"
  1134.     end
  1135. end
  1136.  
  1137. function util:updateSettings(updatedSettings)
  1138.     if initRunning then
  1139.         _p:error("updateSettings - init is running! cannot update settings now.")
  1140.         return
  1141.     elseif shutdownRunning then
  1142.         _p:error("updateSettings - shutdown is running! cannot update settings now.")
  1143.         return
  1144.     elseif not running then
  1145.         _p:error("updateSettings - fly util is NOT running! call 'init' to begin.")
  1146.         return
  1147.     end
  1148.     if updatedSettings and type(updatedSettings)=="table" then
  1149.         _init:applySettings(updatedSettings)
  1150.     end
  1151. end
  1152.  
  1153. return util;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement