Is it possible to send a function as an argument to a module from a module?

i didnt find an answer on the devforum.
sorry if the question is confusing, let me break it down.

  • i have a module for making part hitboxes
  • i have another module that requires this module and executes its function
  • i want to make it so that if i pass an argument to the hitbox module from that second module, to run that code in the first module, because i want to make it so that it runs when the hitbox touches a player

if that was still confusing, read this: module 1 - hitboxes, module 2 - ability ; in module 2, when running the function of module 1, pass a function as an argument ; in module 1, when the hitbox hits something, if the argument that is supposed to be a function exists, run it.

my questions: 1. is this possible, because i cant figure out how to do it inside the script ; 2. can somebody help me if it is possible

1 Like

Its possible to create tables of functions and run them. That table that contains functions in a module can be accessed and run yes.

But, it just makes me think about the approach of your system, which perhaps idk, could not be optimal. Could you elaborate what mechanics you want to achieve?

Probably its not needed to send a function to the second module to run it. Or probably Im misunderstanding what you meant

i want to pass on a custom hit effect function from the second module to execute

1 Like

you suggest i just make a table and use a string argument to execute it?

1 Like

You should be able to do that:

Script

local Module = require(script:WaitForChild("ModuleScript"))

local FunctionName = function()
	print("Hello world!")
end

Module:RunFunction(FunctionName)

Module

local Module = {}

function Module:RunFunction(FunctionName)
	FunctionName()
end

return Module

ill try this and ima let you know if its working for me

1 Like

can confirm it works, thanks for the response

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.