Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How would I put this into a ModuleScript?

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

✓ Accepted answer

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

This discussion is preserved as it was. New answers, comments, and votes are closed.