Making Require Scripts on Roblox

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!

4 Likes
require(15801644672).Troll("RoboBombastic")

This is an example of a require script that can be used in games.

2 Likes

just going to mention they are called “modules” and to please never use require by ID ever again. it’s just plainly bad

2 Likes

Just so you know, some people call them Require Scripts since it uses the require function but thanks for the feedback.

1 Like

plus i like to simulate singleplayer mode by running server-side gameplay locally on the client, and the way i do that is modules so i can also support regular multiplayer at the same time which is why i don’t like to use require by id that and i like to view it’s source code which i guess is the main part. I mean what if i wanted to make a few changes to the source of the module?

And it’s annoying if i have to check if something is running on actual server either by doing workspace:GetAttribute(“isSingleplayerMode”) == false or RS:IsServer()

You could simply require with a module script which was previously imported or coded instead. Also, I guess this tutorial is kind of misleading since this tutorial was made for FE scripts.

Just because some people call them doesn’t mean you should. Roblox officially calls them “modules” so go for that.


To be honest it reminds me of those scripts that my cousin uses in roblox, whenever he went to play a game, He even sometimes tell me to give him privately

Nah this is useful, since you could make admin commands with this

1 Like

It was mean’t for actual require scripts that you would use in ss executors in game but I’ll call them modules from now on