How to make a module refer to the scripts that fire its functions

I want to make a module in server script service able to refer to the scripts that fire the functions inside of it like just making a varuable in the module like:

local humanoid = scrip that fired.Parent.Humanoid

Ive looked around and i can`t see any way to do this, is this possible?

1 Like

You could try sending the script instance itself through the function or the script’s name through the module like this example:

function module.yourFunctionNameHere(scriptReceived)
	local humanoid
	if typeof(scriptReceived) == "string" then
		for i, v in pairs(workspace:GetDescendants()) do
			if v.Name == scriptReceived and (v:IsA("Script") or v:IsA("LocalScript")) then 
				humanoid = v.Parent:FindFirstChild("Humanoid")
			end
		end
	elseif scriptReceived:IsA("Script") or scriptReceived:IsA("LocalScript") then
		humanoid = scriptReceived.Parent:FindFirstChild("Humanoid")
	end
	if not humanoid then return end
    --you can put your code after this
end

This basically firstly checks if the variable scriptReceived is a string (script name) or if it’s an instance.

If it’s a string, then it checks through the workspace for any scripts with the same name and tries to find the humanoid of v.Parent.

If it’s an instance, then it checks if the variable is a script or a local script, and if it is, then it simply tries finding for the humanoid of scriptReceived.Parent.

If it does not find a humanoid, then it just returns nil.

When using this, you can send the script itself as the first argument or the script’s Name through the function to check for the humanoid.

Hope this helped!

As the previous post suggested you could include a ‘script’ parameter in your ModuleScript’s functions that’ll represent whatever script is currently using the ModuleScript when its function(s) is/are called.

--MODULE

local Module = {}

function Module.Example(Script)
	print(Script.Name)
end

return Module
--LOCAL

local Module = require(script:WaitForChild("ModuleScript"))
Module.Example(Script) --LocalScript
1 Like

Thank you this really helpful and it also cleared up some confusion i had with parameters and works well