Module Script function not working

I want to find out how to make and call a function from a “submenu” inside a module script. This is my module script:

local pLib_actions = {
	custom = {
		inPurchase = function(player)
			-- Show some in purchase Gui (When the purchase prompt appears)
		end,

		purchased = function(player)
			-- Thank the player (This will run when a product is purchased)
		end,

		closed = function(userId, assetId, isPurchased)
			-- Close any in purchase Guis (This will run when the prompt is closed)
		end,
	},

    -- The script goes on
}

return pLib_actions

This is just half of the script, the other half is called “default” and it has the same functions but different codes.

This is how I call it (it’s a server script):

local actionSet = config.actions

if actionSet == "default" then
	_G.aS = require(script.Configuration.PurchaseActions).default
else
	_G.aS = require(script.Configuration.PurchaseActions).custom
end

_G.aS.inPurchase(player)

This is where I get the actionSet from: (the actionSet in this case is “default”)

local useCustomActions = configFolder.UseCustomActions

function parse(useCustomActions)
	if not useCustomActions then
		return "default"
	else
		return "custom"
	end
end

local pLib_config = {
	actions = parse(useCustomActions),
    -- Script goes on
}

return pLib_config

My issue is that the function inside the module script doesn’t get called when I want to call it.

Forgot to mention: I’ve tried many different ways to trigger the function and many different ways to define (make) the function.

It looks like the way you’re calling the function from the module script is correct, so there may be a problem with how you’re setting the value of actionSet.

Here are a few things you can try:

  1. Double-check that the value of useCustomActions is being set correctly. You can print out the value to the console to make sure it’s either true or false.
  2. Double-check that config.actions is being set correctly. You can print out the value to the console to make sure it’s either "default" or "custom".
  3. Double-check that the module script is being required correctly. Make sure the file path is correct and that the module is being returned correctly.

If all of these things are correct and you’re still having trouble, you can try adding some print statements inside the functions in the module script to make sure they’re being called at all. For example, you can add print("inPurchase called") inside the inPurchase function to see if it’s being called when you expect it to be.

I hope this helps!

2 Likes

I made the functions print something out once they are called but there is no output from them.

Nevermind, found out the issue, I was checking if useCustomActions existed not for it’s value, turns out it’s been going to custom and since the custom functions are empty, they weren’t outputting. Thanks for helping tho!

1 Like

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