ContextActionService could not find the function passed in, doing nothing

The title is the warning given in the output when I was working on changing my way of coding to a modular system.

I had an Input module. It used to work in the old system, but whenever I work with my newer systems, it doesn’t work and shows the warning (stated in the title). It is like a warning, but the code, still, continues on after the warning is shown in the output.

Here are the code parts:

Input:

   -- Services
   local ContextActionService = game:GetService("ContextActionService")

   local Input = {}
   Input.__index = Input

   function Input:CreateInput(ActionName, Function, ...)
        local Info = {ActionName = ActionName, OldKeyCodes = {...}; KeyCodes = {}; Function = function() end}

        for _, KeyCode in pairs({...}) do
	       Info.KeyCodes[KeyCode] = KeyCode
        end

        Info.Function = Function

        return setmetatable(Info, self)
     end

    function Input:Setup()	
        local function OnKeyPressed(ActionName, UserInputState, InputObject)
	          if not UserInputState == Enum.UserInputState.Begin or not self.KeyCodes[InputObject.KeyCode] or not self.Function then
		          return
	          end
	
	         self.Function()
        end

        local KeyCodesUnpacked = {}
        
     -- Warning may be here, but I cannot wrap this into pcall as 
     -- it is an warning, not an error

        ContextActionService:BindAction(self.ActionName, OnKeyPressed, true, unpack(self.OldKeyCodes))
    end

    function Input:Disconnect()
        ContextActionService:UnbindAction(self.ActionName)
    end

    return Input

Code snippet which uses this module:

            function GuiHandler:OpenGuiInput(GuiName, Callback, ...)
                   Callback = Callback or function() end

                   if RunService:IsServer() then
	                     self:OpenGui(GuiName, Callback, ...)
                   elseif RunService:IsClient() then
                         local OpenedGui = self:OpenGui(GuiName)
                             
                                 -- This part of the code and the one in the legacy
                                 -- exactly the same
	                     local NewInput = Input:CreateInput(GuiName .. "Input", Callback, ...)
	                     NewInput:Setup()

	        return function()
			     NewInput:Disconnect()	
		 	     OpenedGui:Destroy()
		    end, OpenedGui	
    end
end
2 Likes

I noticed that the callback function you used is local. Try making it a global function.

I think this might be because of garbage collection. Once the setup function has finished execution, the function gets garbage collected by Lua, so there isn’t a reference to the function any more. But, if it ends up global, then it avoids garbage collection.

Have you tried using print statements to test out and locate where the warning is occurring?

print("test")
local hi = "hi" (
print("test1")


Output:
test
Unexpected symbol near...

@AbiZinho It’s not yielding, it’s giving a warning. It is stated in the OP that code still runs. A print wouldn’t even be needed in your example because that’d throw an error.


Which lines specifically are causing the error? You wrote a comment in your code saying the error may be occurring in one location, though it’s not really confirmed. Furthermore, check: is it a problem with your first or second script?

You can set up a dummy test for the ModuleScript that uses its methods and see from there whether it’s an issue with your class setup or something else.

I personally doubt the second one has any relevance since it’s a ContextActionService warning, though knowing how the module is being called is surely useful.

I have tested out the Input class separately, it works.
Code:

local Input = require(game.ReplicatedStorage.Util)("Modules", "Input")
Input:Create("InputTest",function() 
      print("Pressed T") 
end,Enum.KeyCode.T):Setup()

I assume the cause lies with what’s being defined as the function then. Your class and unit test didn’t throw anything, so it’s your implementation. This line specifically is the concern, given the warning message:

Callback = Callback or function() end

I would assume that without a defined callback being passed to OpenGuiInput, the blank function is taking charge. Because this function is blank, you are receiving the warning that such is the case. Try filling in your function and see if that dismisses the warning.

Callback = Callback or function() return Enum.ContextActionResult.Pass end

EDIT: This may actually have to be placed in the “return” of OnKeyPressed in the ModuleScript itself - the sink return.

I’ll try and make a repro later if this doesn’t work or isn’t solved by then.

1 Like

All the print statements print, the problem is in self.Function of the Input class. I’ll try to pin-point where it stops

I use the GuiHandler module in my ChestSystem, should I edit my post and include that as well? I think that is also important.

Is your ModuleScript being required server side or client side?

1 Like

I think he’s using both, as the module he requires it in uses run service to find where it’s being run

1 Like

I’ve found out the problem. It seems like OpenGuiInput of the GuiHandler module seemed to cause this warning. I’ve found a potential work-around to this.

1 Like