A_roxn
(aaron)
May 11, 2019, 8:04pm
#1
Hey everyone,
I’m having trouble requiring a module for my moderation scripts. I’m attempting to be able to require the module and have it work inside of a script. When I attempt to do this using:
local ban = require(game.ReplicatedStorage.AutoBan)
the script no longer functions as expected. (The script I’m using to test this is a simple print.)
My module: (Collapsed for screenshot)
How to require a module easily and have functions with it:
--Module
local module = {}
module.doStuff = function(stuff)
end
return module
--Script requiring it
local m = require(module path here)
m.doStuff(1)
For your example:
--Module
local module = {}
module.AutoBan = function(player,reason,length)
--Code here that does stuff to ban players or whatever
end
return module
--Script
local ban = require(game.ReplicatedStorage.AutoBan)
ban.AutoBan(player,reason,length)
If you have anymore questions feel free to ask!
A_roxn
(aaron)
May 11, 2019, 8:13pm
#3
Hey there,
Thanks for your help. Unfortuinately, even with these changes my script still isn’t functional.
Wait, I noticed something that I did wrong by your example
I just tested it in studio. It works. Did you change the module script?
The way I passed a function to my module is easier to use with multiple functions. If you want just one, change the script to:
--Script
local ban = require(game.ReplicatedStorage.AutoBan).AutoBan
ban(player,reason,length)
This will also do the same thing.
A_roxn
(aaron)
May 11, 2019, 8:18pm
#5
One moment, let me test it again with a change from your exmaple I missed.
Sorry.
JakyeRU
(Jakye)
May 11, 2019, 8:18pm
#6
Hello. Try this:
-- Module Script
local Ban = {}
Ban.BanPlayer = function(player, reason, length)
print(player, reason, length)
end
return Ban
-- Script
local BanModule = require(game.ReplicatedStorage.ModuleScript)
BanModule.BanPlayer("JakyeRU", "Exploiting", "3 days")
It should print: JakyeRU Exploiting 3 days
.
A_roxn
(aaron)
May 11, 2019, 8:21pm
#7
Still failing.
My script:
local HttpService = game:GetService("HttpService")
--local discord = require(game.ServerScriptService.Modules.NitroDiscordAPI)
--local Asset = game:GetService("MarketplaceService"):GetProductInfo(game.PlaceId)
local Ban = require(game.ReplicatedStorage.AutoBan).AutoBan
game.Players.PlayerAdded:Connect(function(p)
p.Chatted:Connect(function(msg)
print(msg)
Ban(p,"testing","1 Day")
end)
end)
What is your modules code? Does it print or have any indication to tell you it succeed? How do you know it doesnt work?
Did you change your module? My example only works if you do. The previous way you were using your module wouldn’t work.
A_roxn
(aaron)
May 11, 2019, 8:25pm
#9
I added @JakyeRU ’s script, ignoring my scripts and everything worked.
Thank you for your assistance. I’ll reply here if I bump into anymore issues with this.
snorebear
(snorebear)
May 11, 2019, 8:26pm
#10
Use “:”, not “.” when referencing those functions. Also, what is the error output?
@JakyeRU added a print to my code. I believe you had no print before to indicate success.
Both will work, they just have to both refer to a function.
1 Like
colbert2677
(ImagineerColbert)
May 11, 2019, 8:40pm
#13
No they don’t. Calling a function with dot syntax and with a colon serve two different purposes. A colon passes the object as the first argument self
, dot syntax does not do that. Depending on the parameters you define for your function, this can cause an error to be thrown.
Dot syntax is proper in this case.
cc @snorebear
2 Likes