Context Action Service Binding to UserInputType Touch Movement Issue

I’m creating a track and field game, but your character always has a constant pace, so running games aren’t very enjoyable. Implementing a ‘skill-based’ sprint/stamina is one of the only ways to make the game fun, but as always, I ran into a problem.

On mobile, the way you sprint and use stamina is by tapping the screen. To sprint, you have to be walking, so I bound my action when humanoid.Running, unbound when idle, you get it. For example, I would expect the user to run with their left hand and then butterfly tap as fast as they can with their right hand to get that extra boost to the finish line.

However, the issue is when the player moves with the Dynamic Joystick, the UserInputType.Touch fires, and somehow causes the player to stop moving the joystick (shown in video). I have also tested this on my phone outside of studio and it had the same bug. I have honestly no idea how this happens, but my only guess is that the problem happens when the action is unbound? But why is it unbound when the player is still swiping their finger trying to move?

-- This script is placed in StarterCharacterScripts

local Players = game:GetService("Players")
local ContextActionService = game:GetService("ContextActionService")

local player = Players.LocalPlayer
local character = script.Parent
local humanoid = character:WaitForChild("Humanoid")
local key = "ActionKeyword"

-- Literally any action
local function action(actionName, inputState, obj)
	if actionName == key then
		print("sprint boost")
		return Enum.ContextActionResult.Pass -- Edit: Tried this, still no luck :/
	end
end

-- Making the action only accessible while moving
humanoid.Running:Connect(function(speed)
	if speed > 0 then
		ContextActionService:BindAction(key, action, false, Enum.UserInputType.Touch)
		print("walking")
	else
		ContextActionService:UnbindAction(key)
		print("idle")
	end
end)

Any type of assistance is greatly appreciated, as I could not find any information about this issue elsewhere. If there is any way to get this to function, that would work too, but I would like to use ContextActionService because I already have other keys bound for multiple platforms. I hate consulting the developer forum personally, but I think this should be brought up.

2 Likes

I think the Solution:

The issue was that the Dynamic Joystick was also bounded to the Enum.UserInputType.Touch, so when the character ran on mobile, the dynamic joystick action was getting the data from the touch (literally dragging your finger, locating its position), and immediately after the sprinting action was bound and then called, overriding the dynamic joystick action, stopping it, and boom. That’s my explanation so far, not sure about the overriding part because the dynamic joystick had a higher priority, so idk if that’s a bug, but I just ended up binding it and then checking if the player was moving. Not as efficient, but it works well.

1 Like