ModuleScripts: Clean Scripting by Sharing Functions

With ModuleScripts, you can organize scripts clearly in Roblox and easily share functions between server and client. This way, you avoid duplicate code and make your project easier to maintain.

How to do it:

  1. Create ModuleScript: Create a ModuleScript in the "ReplicatedStorage" folder so that both server and client can access it.
  2. Define functions: Write functions you want to reuse in the ModuleScript and return them at the end.
  3. Load module: Load the ModuleScript in your server or client script using require() to use the functions.
  4. Call functions: Call the functions from the module instead of copying code – this keeps everything clear.
  5. Easy updates: Changes in the module automatically take effect everywhere it is used.

With ModuleScripts, you can structure your Roblox code cleanly and collaborate better.

-- ModuleScript in ReplicatedStorage
local Modul = {}

function Modul.begruessung(name)
    return "Hallo, " .. name .. "! Willkommen im Spiel."
end

return Modul

-- ServerScript or LocalScript
local modul = require(game.ReplicatedStorage:WaitForChild("Modul"))
print(modul.begruessung("Spieler")) -- Outputs greeting