What is causing this function to fire? I'm not even calling it. Possible Bug?

Here is my code, I simplified it and only has the barebones, but the issue pops up regardless:
Code is in a LocalScript container placed in game.StarterPlayer.StarterPlayerScripts

local ContextActionService = game:GetService("ContextActionService")

function A()
	print("Wolf A is not fenced")
end

function B()
	print("Wolf B is not fenced")
end

function C()
	print("Wolf C is not fenced")
	print("Wolf C is enclosed with Wolf D")
end

function D()
	print("Wolf D is not fenced")
	print("Wolf D is enclosed with Wolf C")
end

ContextActionService:BindAction("EncloseA", A, false, Enum.KeyCode.H, Enum.KeyCode.ButtonY)
ContextActionService:BindAction("EncloseB", B, false, Enum.KeyCode.J, Enum.KeyCode.ButtonX)
ContextActionService:BindAction("EncloseC", C, false, Enum.KeyCode.K, Enum.KeyCode.ButtonA)
ContextActionService:BindAction("EncloseC", D, false, Enum.KeyCode.L, Enum.KeyCode.ButtonB)

It seems to call the last BindAction everytime, I spent trying to change up the code and write it differently.

I made errors on my code intentionally, I changed function C and D

function C()
	priadwnt("Wolf C is not fenced")
	pridwant("Wolf C is enclosed with Wolf D")
	Tedwadxt = "Wolf C is not fenced" .. "Wolf C is enclosed with Wolf D"
	print("WHAT?1")
end

function D()
	prwadint("Wolf D is not fenced")
	prwadint("Wolf D is enclosed with Wolf C")
	Texdwt = "Wolf D is not fenced" .. "Wolf D is enclosed with Wolf C"
	print("WHAT?2")
end

It prints this error, notice how it only complains about function D and not C

00:13:19.737 - TTU Home auto-recovery file was created
00:13:30.138 - Go to Game Settings and turn on Allow HTTP requests.
  00:13:30.325 - Players.Formidable_Beast.PlayerScripts.LocalScript:23: attempt to call global 'prwadint' (a nil value)
00:13:30.327 - Stack Begin
00:13:30.328 - Script 'Players.Formidable_Beast.PlayerScripts.LocalScript', Line 23
00:13:30.329 - Stack End
00:13:30.329 - ContextActionService: Unexpected error while invoking callback: Players.Formidable_Beast.PlayerScripts.LocalScript:23: attempt to call global 'prwadint' (a nil value)

I am obviously not trolling, this post took some time to write, it would be a waste of time if I wrote this as a joke.

I restarted studio and reset all settings to try and fix it. I run the script on an empty place with only the localscript inside. I haven’t tried unplugging all input devices yet(i don’t want to mess with wires). I did unplug my controller to see if it was the issue. I don’t know how to use call stack, but it does get called when I start the game. I used a watch to track variables.

2 Likes

This is strange, the only way I was able to stop it from occurring was by removing Enum.KeyCode.ButtonB in your code. I was then able to re-produce this bug simply by using this:

local ContextActionService = game:GetService("ContextActionService")

local function callback()
	print("hello")
end

ContextActionService:BindAction("Action", callback, false, Enum.KeyCode.ButtonB)

For some reason binding anything to Enum.KeyCode.ButtonB will call the function once when the game starts. On another note you have named two of your actions “EncloseC”.

I suggest making a bug report in #platform-feedback:engine-bugs. (Unless I’m wrong)

2 Likes

Ok so I tested your code in studio and was able to replicate the error your talking about, however upon inspection I notice your code was binding the functions incorrectly, upon fixing all of your mistakes they work as expected:

local ContextActionService = game:GetService("ContextActionService")

function A(Action,State,Input)
	if Action == "EncloseA" and(State == Enum.UserInputState.Begin or State == Enum.UserInputState.End) then
		print("Wolf A is not fenced")
	end
end

function B(Action,State,Input)
	if Action == "EncloseB" and(State == Enum.UserInputState.Begin or State == Enum.UserInputState.End) then
		print("Wolf B is not fenced")
	end
end

function C(Action,State,Input)
	if Action == "EncloseC" and(State == Enum.UserInputState.Begin or State == Enum.UserInputState.End) then
		print("Wolf C is not fenced")
		print("Wolf C is enclosed with Wolf D")
	end
end

function D(Action,State,Input)
	if Action == "EncloseD" and(State == Enum.UserInputState.Begin or State == Enum.UserInputState.End) then
		print("Wolf D is not fenced")
		print("Wolf D is enclosed with Wolf C")
	end
end

ContextActionService:BindAction("EncloseA", A, false, Enum.KeyCode.H, Enum.KeyCode.ButtonY)
ContextActionService:BindAction("EncloseB", B, false, Enum.KeyCode.J, Enum.KeyCode.ButtonX)
ContextActionService:BindAction("EncloseC", C, false, Enum.KeyCode.K, Enum.KeyCode.ButtonA)
ContextActionService:BindAction("EncloseD", D, false, Enum.KeyCode.L, Enum.KeyCode.ButtonB)

Ok so first your last two lines have the same action key, “EncloseC”, i changed it to EncloseD i assumed it was a typo. Next of all when your binding functions to the CAS you need to specify the correct parameters to ensure that the input being passed to them correctly.

Next, So you may be wondering why each statement printed twice, the function is bounded to both the input begin and input end state (meaning when they press the key and when they are done pressing the key) , if you just want it to be the input began or vice versa then you can modify the functions accordingly, like this:

--change this line in all functions
if Action == "EncloseD" and(State == Enum.UserInputState.Begin or State == Enum.UserInputState.End) then
--to this if you want it to only be connected when they press the key:
if Action == "EncloseD" and State == Enum.UserInputState.Begin then
--or this for when the key is finished being pressed
if Action == "EncloseD" and State == Enum.UserInputState.End then

Note that after fixing these mistakes all the functions worked as expected in studio, here is the link to the CAS:BindAction() API : https://developer.roblox.com/en-us/api-reference/function/ContextActionService/BindAction

4 Likes

Thank you, it works now.

However I wonder why I have to test for the parameters, I assumed that CAS will fire functions if a specific button is pressed, like an event listener. I assumed that it will never fire since I haven’t press any button or the key binding itself. I thought that InputObjects are only needed in UIS to test for parameters. I forgot to never assume that everything runs perfectly, I guess. This issue leaves even more questions for me…

2 Likes

I guess that’s why they have the action name parameter, in order to verify the input

2 Likes