I am trying to connect to the ContextActionService to make my game playable with KBM and Controller however I am having an issue with my dash ability not working specifically with the ButtonA keycode for controller. It works with literally any other key I tried on my controller.
I am using an official PS4 controller for testing.
I saw another post about the UserInputService however I am not having that issue, I don’t think. Here is my code:
--- Run Context: Client
--- Services is already defined.
Services.ContextAction:BindAction("movement", movementHandler, false,
Enum.KeyCode.Space,
Enum.KeyCode.LeftShift,
Enum.KeyCode.ButtonA)
--- Run Context: Client
function movementHandler(actionName: string, inputState: Enum.UserInputState, inputObject)
if inputState == Enum.UserInputState.End then
if inputObject.KeyCode == Enum.KeyCode.Space or inputObject.KeyCode == Enum.KeyCode.ButtonA then
reDash:FireServer()
elseif inputObject.KeyCode == Enum.KeyCode.LeftShift then
reSprint:FireServer()
end
end
end
--- Run Context: Server
function dashHandler(player: Player)
print("Dash Server")
local bvPushingForce = Instance.new("BodyVelocity")
local mdCharacter = player.Character
local humHumanoid: Humanoid = mdCharacter:FindFirstChild("Humanoid")
local bphumanoidRootPart: BasePart = mdCharacter:FindFirstChild("HumanoidRootPart")
local animator = humHumanoid:FindFirstChildOfClass("Animator")
if animator then
local dashAnimation = Instance.new("Animation")
dashAnimation.AnimationId = "rbxassetid://15930101859"
local animationTrack = animator:LoadAnimation(dashAnimation)
if animationTrack then
animationTrack:Play()
end
end
local vecMovementDirection = bphumanoidRootPart.CFrame.LookVector
bvPushingForce.Velocity = vecMovementDirection * 150 + Vector3.new(0, 5, 0)
bvPushingForce.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
bvPushingForce.Parent = bphumanoidRootPart
wait(0.5)
bvPushingForce:Destroy()
end
again this works with literally any other keycode other than ButtonA, it doesn’t even print Dash Server, which leads me to believe the event isn’t being fired for some reason?
EDIT:
If push comes to shove, I have a ContextActionService replacement that I wrote in the Community Resources forum. The setup is a little different, but the actual function calls are the same, so you shouldn’t need to perform any changes to the actual handlers.
This doesn’t help because the Space bar is also bound to jump however I can still use the dash ability. I have also disabled jumping entirely for players. I don’t know why specifically ButtonA isn’t working.
Put this in a local script on a new baseplate and try it:
local userInputService = game:GetService("UserInputService")
userInputService.InputBegan:Connect(function(inputObject, processed)
if inputObject.KeyCode == Enum.KeyCode.ButtonA then
print("Button A pressed.")
end
end)
userInputService.InputEnded:Connect(function(inputObject, processed)
if inputObject.KeyCode == Enum.KeyCode.ButtonA then
print("Button A released.")
end
end)
If it’s printing, then the problem is something to do with your scripts. If it’s not printing, then there’s some issue outside of Roblox, or it could be Roblox itself.
Since you’re checking the input’s KeyCode inside of your movementHandler function anyways, I’d recommend you use UserInputService instead of ContextActionService by doing this:
local UserInputService = game:GetService("UserInputService")
local function onInputBegan(input, gameProcessedEvent)
if gameProcessedEvent then return end
if input.KeyCode == Enum.KeyCode.Space or input.KeyCode == Enum.KeyCode.ButtonA then
reDash:FireServer()
elseif input.KeyCode == Enum.KeyCode.LeftShift then
reSprint:FireServer()
end
end
UserInputService.InputBegan:Connect(onInputBegan)
When using ContextActionService, you’re meant to check either the InputState (you always need to check the InputState, actually) or the action’s name, I’ll write an example that uses ContextActionService if you prefer
Edit: @tannnxr I tested this example and it should work to achieve what you want:
local ContextActionService = game:GetService("ContextActionService")
local function movementHandler(actionName, inputState)
if inputState == Enum.UserInputState.Begin then
if actionName == "dash" then
reDash:FireServer()
elseif actionName == "sprint" then
reSprint:FireServer()
end
end
end
ContextActionService:BindAction("dash", movementHandler, false, Enum.KeyCode.Space, Enum.KeyCode.ButtonA)
ContextActionService:BindAction("sprint", movementHandler, false, Enum.KeyCode.LeftShift)
Okay so I wrote some code for UserInputService but now it’s still not working, did I do something wrong here?
Context: Space works, ButtonA doesnt.
print("InputManager Loaded")
local Services = require(script:WaitForChild("Services"))
local serReplicatedStorage = game:GetService("ReplicatedStorage")
local dirEvents = serReplicatedStorage:WaitForChild("Events")
local reDash = dirEvents:WaitForChild("Dash")
local reSprint = dirEvents:WaitForChild("Sprint")
function movementHandler(inputObject, gameProcessed: boolean)
if gameProcessed then return end
if inputObject.UserInputType == Enum.UserInputType.Keyboard or inputObject.UserInputType == Enum.UserInputType.Gamepad1 then
if inputObject.KeyCode == Enum.KeyCode.Space or inputObject.KeyCode == Enum.KeyCode.ButtonA then
reDash:FireServer()
end
end
end
Services.UserInput.InputBegan:Connect(movementHandler)