:BindAction also runs the function therein

I know what you’re thinking!
You think I left the parenthesis in, like this:

ContextActionService:BindAction("BeginLevel", startLevel(), false, Enum.KeyCode.E)

But no.

I am trying to bind an action which will make something not cancollide, but for testing purposes, “BeginLevel” unbinds the action and prints “Key E Pressed!

However, in a different function which is called based off of a touched event prints E before I have hit the key!

Here are some code samples:

restartLevel.OnClientEvent:Connect(function(levelToSet) -- The remote event fires from a seperate hitbox touched event.
	
	restartLevelFunction(levelToSet)
	
end)

Then:

local function restartLevelFunction(levelToRestart)
	
	-- Do some stuff to set up the level
	
	contextActionService:BindAction("BeginLevel", startLevel, false, Enum.KeyCode.E)
	
end

Then:

local function startLevel()
	
	contextActionService:UnbindAction("BeginLevel")
	
	print("Key E pressed!")
	
end

Here is a gif of the problem. The Localization Tools error is a studio bug, as you probably already know if you are reading this soon after me writing it.

https://gyazo.com/0556a000ff313d50358c4e3a9707241e

Thanks in advance!

startLevel() will be called on the key press (Begin) and again on key release (End) –
Since you are Unbinding in startLevel() itself, you’re probably getting a Cancel inputState.
You need to check the inputState passed to startLevel() so you should probably have:
local function startLevel( actionName, inputState, inputObject )
Edit: Capitalization

2 Likes

Also, the Developer Hub page for ContextActionService:BindAction has examples and goes more in-depth on what all it does and what you can do with it.

1 Like