Introduction
Welcome to my tutorial! This tutorial is for creating Require Scripts and using the require function for importing certain scripts.
To start off, a require script is a line of code used in game to import a certain GUI or even a custom character. In this tutorial, I will provide a step by step tutorial on how to make a require script.
The Module Script
The module script is an essential for coding a Require Script. To start, let’s add a model script in the Workspace section. Name the ModuleScript to MainModule. Enter the module script and type out the following:
local module = {}
function module.fire()
print("Module Script Required")
end
return module
These lines of code won’t do anything at first, but try using a script or go inside the developer console and use the following code:
require(workspace.ModuleScript):fire()
This code will activate the function inside the module and will print “Module Script Required” in the output (If you didn’t change the string inside the quotation marks). Now, to give a certain GUI to a player, we’ll need the player, so how do we get that? It’s simple, put the variable inside the function’s parentheses! Put in a new variable inside the function as we will require that for when we put the GUI inside the player’s PlayerGui folder. Here’s the code that we have currently:
local module = {}
function module.fire(player)
print("Module Script Required")
end
return module
We still haven’t gave the GUI to the player, so we’ll need to do that next. You can code your GUI as usual and make the parent of your GUI the module script once finished with your GUI. Delete the print function once inside the Module Script. Here’s the next code:
local module = {}
function module.fire(plr)
local gui = script.ScreenGui:Clone() -- Replace "ScreenGui" with the name of your gui
local player = game.Players:FindFirstChild(plr) -- plr is a string
gui.Parent = player.PlayerGui
end
return module
You can also make multiple of a function, like this:
local module = {}
function module.fire(plr)
local gui = script.ScreenGui:Clone() -- Replace "ScreenGui" with the name of your gui
local player = game.Players:FindFirstChild(plr) -- plr is a string
gui.Parent = player.PlayerGui
end
function module.water(plr)
print("Water")
end
return module
I believe you get the point here. To test this, create a script or go inside the developer console in your game and run the following code:
require(workspace.MainModule).fire("Username") -- Replace username with your username
Publishing and Using Your Require Script
It’s pretty simple to use your script! Right click your Module Script and publish it to Roblox. You can name it anything you want but be sure to submit it. Once you submit it, copy the ID of your creation. The ID is essential for using a require script as without this ID, you cannot use your script.
Next, be sure to format your script like this:
require(ID HERE).fire("Username")
Replace ID with the ID you copied from publishing your script, replace .fire with the name of your function (if you ever changed .fire to any other function name) and replace Username with yours or another player if you’re feeling a bit generous today.
This final step raps up this tutorial, I hope this helped! Thanks for using this tutorial!