What do you want to achieve? Keep it simple and clear!
i’m trying to make a function run whenever another one does, but i found out the :connect() only works for rbx script signals, rather than functions. Basically, i want to make a module script that when intialized with a function as a parameter, it connects that function to a function within the module script, making it run whenever the function does.
What is the issue? Include screenshots / videos if possible!
What solutions have you tried so far? Did you look for solutions on the Developer Hub?
i tried looking for other solutions, and found bindable event, but its not quite what i’m looking for as it doesn’t really seem optimal because it requires the instance & it firing it every event and its hard to really keep track, and also i can’t really unbind it in the future easily (while i don’t need it for this specific script, i might need it in the future).
After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!
--Here is function in module script:
function stam:Init(player: {Player}, Actions, Params)
for action, value in pairs(Actions) do
value["Function"]:Connect(self:TakeStamina(player, value["Cost"]))
end
--Here is it in local script:
local dodge = function()
-- do something
end
StaminaModule:Init(player, {Dodge = {Function = dodge, Cost = 30}})
It’s not possible to connect functions to functions, but you can use Roblox’s BindableEvent instance or a module which is an alternative to it. Try creating that instance inside of the module called “takeStamina” or something as a variable in the module script and expose it to the local script via the table returned by the module when initialized. Then, you could fire the event directly from the local script once you’ve connected it to another function which calls the self:TakeStamina() function. Roblox’s event objects can pass arguments into functions connected to it, so you could just put the params table into it.
Alternatively, if you want to directly call functions once a function gets called, you can make the module script return a wrapper function, so it would call both functions, for example:
--module:
function stam:Init(player:{Player}, Actions:{[string]:{["Function"]:(any),["Cost"]:number}}, Params:{any})
local wraps = {}
for actionName, actionValue in pairs(Actions) do
local wrap = function()
self:TakeStamina(player, actionValue["Cost"])
task.spawn(actionValue["Function"])
end
wraps[actionName] = wrap
end
return wraps
end
--local script:
local dodge = function()
--do something
end
local skill = function()
--do something
end
local wrappedFunctions = StaminaModule:Init(player, {
Dodge = {Function = dodge, Cost = 30},
Skill = {Function = skill, Cost = 40}
})
--call them
wrappedFunctions["Dodge"]()
wrappedFunction["Skill"]()