Binded function of BindAction(), fires once when the script first runs, and doesn't work again

I recently was connecting the R key, to a function, using ContextActionService:BindAction().

I am almost certain that this is a roblox bug,as I have plenty of experience with ContextActionService, but decided to make a Scripting Support post to confirm it.

Code:

Function:

function RPressed(actionName, inputState)
	if inputState == Enum.UserInputState.Begin then
		print("YEET")
	end
end

Connection:

ContextActionService:BindAction("RPressed", RPressed, false, Enum.KeyCode.R)

The issue is, that when the game begins, RPressed() is fired once by the BindAction, even though I have pressed no keys.

When I start the game with the code I posted above, “Yeet” is printed into the output:
image

After firing the function once, the BindAction() stops working, and does not fire RPressed() again when I press the R key. The BindAction() triggers the function when the script starts, and renders useless after that. This only happens about 50% of the time.

Before I create this topic, I wanted to mention that I did find a solution to this.
If I add a wait(3)( Which I am assuming is about the time it takes the game to fully load. I tried using game.Loaded:Wait(), though, and it doesn’t fix the issue. ), directly before calling BindAction(), the code works as intended. I have no idea why this is the case, but it does fix the problem. However, it is extremely strange that I have to do this at all, so I am hoping to find a solution that makes more sense, and is not dependent on waiting an obscure amount of seconds.

Does anyone have any ideas of what is causing this?
Thanks for reading!

Hmm. What’s the Parent? ReplicatedFirst?

No, it is a child of a Tool object. It’s not a problem with the script’s environment, as it is running perfectly fine.

1 Like

Where is the Tool? Are you manually parenting it to the Backpack on PlayerAdded?

Interesting–Someone else is complaining about the ‘R’ key:

1 Like

It’s a tool being parented to the backpack via the StarterPack.

I swapped the R key out for the T key, and it works fine( lol ). Not quite sure what to do, as the R key is a necessity in the project I’m working on.

In the mean time, I suppose I’ll just make do with UserInputService, but I hope someone can explain the cause of this at some point.

Thanks for your help trying to solve this issue!

You’re welcome. It’s been added to Studio Bugs - Developer Forum | Roblox
at least.

2 Likes