Advertisement
Cakey3101

Module Loader

Jun 7th, 2025
262
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.04 KB | Source Code | 0 0
  1. local Players = game:GetService("Players")
  2. local ServerScriptService = game:GetService("ServerScriptService")
  3. local RunService = game:GetService("RunService")
  4. local StarterPlayer = game:GetService("StarterPlayer")
  5. local StarterPlayerScripts = StarterPlayer.StarterPlayerScripts
  6.  
  7. local IsServer = RunService:IsServer()
  8. local RootDirectory = if IsServer then ServerScriptService else Players.LocalPlayer:WaitForChild("PlayerScripts")
  9. local ModuleDirectory = if IsServer then RootDirectory.Services else RootDirectory:WaitForChild("Controllers")
  10.  
  11. local function RequireModule(Module: ModuleScript)
  12.     if not Module:IsA("ModuleScript") then
  13.         return
  14.     end
  15.  
  16.     local Import = require(Module)
  17.  
  18.     local OnStart = Import.OnStart
  19.     if OnStart then
  20.         task.spawn(OnStart)
  21.     end
  22. end
  23.  
  24. return function()
  25.     for _, Descendant: ModuleScript in ModuleDirectory:GetDescendants() do
  26.         RequireModule(Descendant)
  27.     end
  28.  
  29.     if not IsServer then
  30.         ModuleDirectory.DescendantAdded:Connect(function(Descendant: ModuleScript)
  31.             RequireModule(Descendant)
  32.         end)
  33.     end
  34. end
  35.  
Tags: robloxstudio
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement