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)
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
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.
@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.