ContextActionService not binding action?

I’m making a script that controls jumping, because I want to precisely control when players can and cannot jump. I’m using ContextActionService to bind the CharacterJump PlayerAction to a function called “jump”, at priority 1000 which should be lower (i.e. prioritized higher) than the default jump action, which is at 2000.

This should result in the “jump” function being called every time the player sends input to make them jump, e.g. pressing the space bar. However, sometimes this just does nothing. In studio it occasionally works, and I have no idea what causes it to do so. When testing in-game, it never works.

Here is the relevant part of the script:
function jump(_, inputState)
	print("jumping", inputState)
	if inputState == Enum.UserInputState.Begin then 
		--more jumping related logic
	end
end

function initialize()
	spawn(function()
		local playerScripts = player:WaitForChild("PlayerScripts")
		local controlScript = playerScripts:WaitForChild("PlayerModule")
		ActionService:BindActionAtPriority("jumpAction", jump, false, 1000, Enum.PlayerActions.CharacterJump)
		print("bound")
	end)
	
	RunService.RenderStepped:Connect(function()
		--some logic checking if the player should be able to jump
	end)
end

initialize()

I put in the print("bound") statement to verify that the game actually reaches that point in the script. This always prints, even if nothing works and the binding doesn’t show up in the developer / F9 console. When binding doesn’t work, the print statement inside jump() runs, printing jumping Enum.UserInputState.Cancel to the console.

I can’t continue working on my game as long as this keeps happening, and I have no idea what might be causing it. I tried binding to KeyCode.Spacebar instead if CharacterJump, as well as using BindAction instead of BindActionAtPriority. I even tried binding at priority 3000, in case I got the way that works wrong. None of those made any difference. I also tried putting the jumping script in another place entirely, to verify that it’s not another script messing stuff up. The exact same happens in the new place.

You can try it out for yourself at Boat Game - Roblox

I really hope someone can help me, it’s been bugging me for a while now and I’m at my wits’ end here :frowning:

Try to set priority to 1 as 1000 can be pretty high? See what happens.

I’ve now tried binding at priorities 1000, 10, 1, 3000 and 10000. Still doesn’t work.

If the player’s character’s humanoid’s JumpPower is set to 0, the user is unable to jump.

You can also make it event based by using the Humanoid.Jumping event.

local CanJump = false

Humanoid.Jumping:connect(function()
    if CanJump == false then
        Humanoid.Jump = false
    end
end)

It seems you are doing a lot more than you have to in order to prevent the humanoid from jumping, this may be a simpler solution.

When I tested your game, it worked fine for me?

1 Like

Seems like the Jump PlayerAction gets canceled when you bind it. But running the code you posted above with Enum.KeyCode.Space bound instead works fine for me.

To be honest, even if it worked, I wouldn’t use the player action bindings. i’m not sure if they’re deprecated, but even in the player scripts they aren’t used anymore.

So to be clear, I ran this code in a LocalScript in PlayerScripts and it worked:

local cas = game:GetService("ContextActionService")

function jump(_, inputState)
	print("jumping", inputState)
	if inputState == Enum.UserInputState.Begin then 
		--more jumping related logic
	end
end

function initialize()
	local player = game.Players.LocalPlayer
	local playerScripts = player:WaitForChild("PlayerScripts")
	spawn(function()
		local playerScripts = player:WaitForChild("PlayerScripts")
		local controlScript = playerScripts:WaitForChild("PlayerModule")
		cas:BindActionAtPriority("jumpAction", jump, false, 1, Enum.KeyCode.Space)
		print("bound")
	end)
end

initialize()
1 Like

The CoreScripts actually still do use action bindings and the PlayerActions Enum for player movement. They also do use UserInputService for different actions.

I can’t get it to break again. I guess that means my issue is solved? Still, super frustrating -.-

Thanks to everyone who tried to help.