Major thanks to Discord user FleshyOverlord for originally making this.
SNIPPET TITLE: Lua Require() example by @FleshyOverlord
SKILL LEVEL: Easy
WHAT DOES IT DO?:
Require() essentially lets you embed a script into other scripts. This means you can access the script's functions in either script.
EXAMPLE USE CASES:
This is useful for when you want to have a script that you use in multiple places, but only want to update once when making changes. For instance, you can have a script that generates projectiles based on a bunch of properties, and then drop that script into multiple weapons. Each weapon call call the original script's functions, and you only have to change it once to update them.
BASE SNIPPET
GreetingsModule.lua
--Create an object that can be grabbed using "require"
local API = {}
--A simple function that prints "Hello World"
function API.PrintGreetings()
print("Hello World")
end
--Return the "API" object so that it can be required
return API
RequireTest.lua
--Asset Reference to the "GreetingsModule.lua" file
local requiredScript = script:GetCustomProperty("GreetingsAPI")
--Pull the API module from the "GreetingsModule.lua" script
local GreetingsAPI = require(requiredScript)
--Call the "PrintGreetings" funnction from the "API" module
GreetingsAPI.PrintGreetings()