Advertisement
TaylorsRus

Simple Framework (LuaU)

Mar 3rd, 2024
729
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.77 KB | None | 0 0
  1. local ReplicatedStorage = game:GetService("ReplicatedStorage")
  2. local Shared = require(ReplicatedStorage:WaitForChild("Shared"))
  3. local Services = script:WaitForChild("Services")
  4. -- // A "Service" simply returns to a server-sided script.
  5.  
  6. local RequiredServices = {}
  7.  
  8. for _,Service in ipairs(Services:GetDescendants()) do
  9.     -- // We use ipairs because GetChildren() returns an array, which has Index = Value pairs. ipairs.
  10.     if Service.ClassName ~= "ModuleScript" then
  11.         continue
  12.         -- // Continue forces a loop to it's next iteration, it is an alternative to;
  13.         --[[
  14.             if Condition then
  15.                 -- Code
  16.             end
  17.            
  18.             -- // This is called indentation reduction.
  19.         ]]
  20.     end
  21.  
  22.     RequiredServices[Service.Name] = require(Service)
  23.     -- // ex; ServiceTemplate = {Init = function(), Start = function()}
  24. end
  25. -- // The reason we run this loop is because we must iterate through all the services twice.
  26. -- // Once to run all Init() functions, and once those are done, again to run Start() functions.
  27. -- // The point of Init() functions is to setup things that will be used in other scripts, in the other scripts Start().
  28.  
  29. _G.GetService = function(Service)
  30.     for Name, Module in RequiredServices do
  31.         if Name == Service then
  32.             return Module
  33.         end
  34.     end
  35.  
  36.     error("Could not find service :"..Service)
  37. end
  38. for Side, Table in Shared do
  39.     if Side == "Client" then
  40.         continue
  41.     end
  42.  
  43.     for Name, Function in Table do
  44.         _G[Name] = Function
  45.     end
  46. end
  47.  
  48. for Name, Service in RequiredServices do
  49.     if Service.Init then
  50.         Service.Init()
  51.     end
  52. end
  53.  
  54. for _,Service in RequiredServices do
  55.     if Service.Start then
  56.         Service.Start()
  57.     end
  58. end
  59.  
  60. for _,Service in RequiredServices do
  61.     if Service.Secondary then
  62.         Service.Secondary()
  63.     end
  64. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement