Using module scripts to load functions

So I’m trying to use a module script to make do some functions stuff. So basically I want to require the module script and use it as a function, not sure how to explain that but yeah. How do I make it work???

(it’s kinda messy)

the module script:

local module = {}

function Immobulus()
	local SpellCastPart = script.Parent.Parent:FindFirstChild("SpellCastPart")
	local TweenService = game:GetService("TweenService")
	local Touched = false
	
	local Part = Instance.new("Part")
	Part.Shape = "Ball"
	Part.Name = "ImmobulusPart"
	Part.Parent = workspace.SpellDebris
	Part.Position = SpellCastPart.Position
	Part.Size = Vector3.new(0,0,0)
	Part.Anchored = true
	Part.Color = script.Colour.Value
	Part.Transparency = script.Transparency.Value
	Part.CanCollide = false
	
	local TweenInfo1 = TweenInfo.new(
		script.TweenTime.Value, 
		Enum.EasingStyle.Sine, 
		Enum.EasingDirection.Out,
		0,
		false,
		0
	)
	
	local TweenGoals1 = {
		Size = script.TweenSize.Value;
	}
	
	local Tween1 = TweenService:Create(Part, TweenInfo1, TweenGoals1)
	Tween1:Play()
	
	Part.OnTouched:Connect(function(Player)
		if Touched then
			Touched = true
			local Player = game:GetService("Players"):GetPlayerFromCharacter(Player)
			local Humanoid = Player.Character:FindFirstChild("Humanoid")
			Humanoid.PlatformStand = true
			wait(script.DisableTime.Value)
			Touched = false
			Humanoid.Sit = true
		end
	end)
end

return module

the server script:

local RepStorage = game:GetService("ReplicatedStorage")

--spell handling
local function ImmobulusFunction()
	local ImmobulusModule = script.Parent.Parent.Spells:FindFirstChild("Immobulus")
	local ImmobulusSpell = ImmobulusModule.Immobulus()
	local ImmobulusRequire = require(ImmobulusSpell)
	ImmobulusRequire()
	print("required")
end
local ImmobulusRemote = RepStorage.SpellRemotes:FindFirstChild("Immobulus")
ImmobulusRemote.Event:Connect(ImmobulusFunction)
1 Like

Ok, I might be hard at explaining but I’ll try.

You have the basics done like inserting a function in the module, good job. Now let’s go on to requiring the module.

local module = require(module) -- Requiring it from the module script.

module.print() -- Let's say there is a print function, this is how you call it.

Now for your script.

Instead of doing this:

local function ImmobulusFunction()
	local ImmobulusModule = script.Parent.Parent.Spells:FindFirstChild("Immobulus")
	local ImmobulusSpell = ImmobulusModule.Immobulus()
	local ImmobulusRequire = require(ImmobulusSpell)
	ImmobulusRequire()
	print("required")
end

Do:

local function ImmobulusFunction()
	local SpellModule = require(script.Parent.Parent:WaitForChild("Spells"))
    SpellModule.Immobulus()
	print("required")
end
1 Like

Alright, thanks. But how do I make it do the function inside the module, should I take it out of the function?

You have to do:

module.Immobulus = function()

In the module as you are returning the table of spells.

Wait I’m confused, I still don’t understand how I run whats inside the module.

And now I have this what can I put inside it?

	ImmobulusRequire.Immobulus = function()
		
	end

Sorry for the confusion, the function should be inside the module script.

function Immobulus()
	local SpellCastPart = script.Parent.Parent:FindFirstChild("SpellCastPart")
	local TweenService = game:GetService("TweenService")
	local Touched = false
	
	local Part = Instance.new("Part")
	Part.Shape = "Ball"
	Part.Name = "ImmobulusPart"
	Part.Parent = workspace.SpellDebris
	Part.Position = SpellCastPart.Position
	Part.Size = Vector3.new(0,0,0)
	Part.Anchored = true
	Part.Color = script.Colour.Value
	Part.Transparency = script.Transparency.Value
	Part.CanCollide = false
	
	local TweenInfo1 = TweenInfo.new(
		script.TweenTime.Value, 
		Enum.EasingStyle.Sine, 
		Enum.EasingDirection.Out,
		0,
		false,
		0
	)
	
	local TweenGoals1 = {
		Size = script.TweenSize.Value;
	}
	
	local Tween1 = TweenService:Create(Part, TweenInfo1, TweenGoals1)
	Tween1:Play()
	
	Part.OnTouched:Connect(function(Player)
		if Touched then
			Touched = true
			local Player = game:GetService("Players"):GetPlayerFromCharacter(Player)
			local Humanoid = Player.Character:FindFirstChild("Humanoid")
			Humanoid.PlatformStand = true
			wait(script.DisableTime.Value)
			Touched = false
			Humanoid.Sit = true
		end
	end)
end

Here, you really did not return the function so change function Immobulus() to module.Immobulus = function()

You could also just do: (in the module script)
function module.Immobulus()
–code you have
end

Then in the server script just do what Quwanterz said a few posts up.

1 Like

alright thanks I finally got it working, shame you can’t give 2 solutions out.

It’s fine, I couldn’t care less about solutions. But for future reference, I believe Quwanterz solution for you was to do:

module.Immobulus = function()
    ---Code here
end

Alternatively, you can do something like this:

-- ModuleScript
return function()
    -- code
end
--- Script/LocalScript
local MyFunction = require(pathToModule)

MyFunction()

To add to this, in Lua, it’s essential to be able to distinguish between using a dot . or using a colon : when working with functions like this. This lua.org site does a very good job at explaining the difference between the two operators, but if you don’t quite get it and want to be safe, just use a colon instead of a dot.

Like this:

function module:Immobulus()

@detourious is also right; ModuleScripts are highly flexible and can return any type of value. Whatever you pass into that return statement will be outputted when you use require() on a module.