im new to using modulescripts and I would like to return what a brick touches using a modulescript, and then passing it along to another script when said script “requires” the modulescript.
Yes
You can hook a function to a .Touched
event after the ModuleScript
is required from a normal script.
Passing the value would be setting the module’s variables to the hit. Maybe in a table, if you’re using multiple. Simply use ModuleScript.Variable
to pass it.
There was one unclear thing: What is the goal of using the ModuleScript
?
Could you provide a little more information on what you are indenting to do as I am slightly confused to why you want a ModuleScript to check if a part has been touched.
Lets start by explaining what modules are. Modules are containers that essentially store code until it is required by a script. This is very useful because you can write one function and then use it in multiple different scripts making that functions easier to update later on.
From my understanding of your question I don’t think using a module to detect if the part has been touched is the best option but it would really depend on your use case is. I would have the ModuleScript do whatever needs to be done and have the script detect if the part has been touched. Here is a little example:
-- Script
local ModuleScript = require(script.Parent.ModuleScript) -- Requires the module
game.Workspace.Part.Touched:Connect(function(hit) -- Checks if the part has been touched
ModuleScript.TakeDamage(hit) -- Fires the TakeDamage function in the module.
end)
-- ModuleScript
local module = {}
function module.TakeDamage(hit) -- The function fired by the script
local Character = hit.Parent
Character.Humanoid:TakeDamage(20)
end
return module
The ModuleScript’s purpose is to create a touched connection and return the argument “hit” that’s supplied to Touched. The hit argument can just be passed around by reference.