Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
2

How would I put this into a ModuleScript?

Asked by
Zerio920 285 Moderation Voter
10 years ago

I'm trying out module scripts for the first time to increase efficiency in my game, but they seem a little complicated.

So, for example, I use this a lot:

script.Parent.Touched:connect(function(hit)
    if hit.Parent:FindFirstChild("Humanoid") then
        -- code
    end
end)

Could I put any of this into a module script? And what would it look like? So instead of having to type out all that I could just do maybe something like

require(TouchedScript) require(HumanoidCheck)

2 answers

Log in to vote
3
Answered by
Nymint 85
10 years ago

To my knowledge, require is used outside of the modulescript. I still don't exactly know how to use it, but I still use it as a function library. I am not sure how to use it for EVENTS, though...

For example

ModuleScript

local Library={}

function Library.CheckHumanoid(Model)
if Model:FindFirstChild("Humanoid") then
return true else return false end end

return Library

ServerScript, inside of a Part with .Touched Event

local Library=require(Workspace:WaitForChild("ModuleScript"))
local Part=script.Parent

Part.Touched:connect(function(hit)
if Library.CheckHumanoid(hit.Parent)==true then
hit.Parent:BreakJoints()
end end)
Ad
Log in to vote
1
Answered by
l0cky2013 135
10 years ago
--modulescript
local t={};


function t.connectTouch(part, func)
local con; 
con=part.Touched:connect(func)
end

return t;
--script


local module=require(Workspace.Blah)--rename it
module.connectTouch(script.Parent, function(hit) print(hit.Parent.Name) end)

Something like that would work.

Answer this question