ContextActionService using last action

Im tryna make a simple dashing system, but when i dont press anything and press Q it just does the last thing i did instead of DashNeutral

local uis = game:GetService("UserInputService")
local cas = game:GetService("ContextActionService")

local plr = game:GetService("Players").LocalPlayer
local Character = plr.Character
local hum = Character:FindFirstChildOfClass("Humanoid")
local humRP = hum.RootPart

local function Dash(actionName, inputState, _inputObject)
	if actionName == "DashNeutral" and inputState == Enum.UserInputState.Begin then
		local ForceMulti = 3000
		humRP:ApplyImpulse(humRP.CFrame.LookVector * ForceMulti + Vector3.new(0, 0, 0))

		print("Nforce")
	elseif actionName == "DashForwards" and inputState == Enum.UserInputState.Begin then
		local ForceMulti = 3000
		humRP:ApplyImpulse(humRP.CFrame.LookVector * ForceMulti + Vector3.new(0, 0, 0))

		print("Wforce")
	elseif actionName == "DashLeft" and inputState == Enum.UserInputState.Begin then
		local ForceMulti = 3000
		humRP:ApplyImpulse(humRP.CFrame.RightVector * math.abs(ForceMulti) * -1 + Vector3.new(0, 0, 0))

		print("Aforce")
	elseif actionName == "DashBackwards" and inputState == Enum.UserInputState.Begin then
		local ForceMulti = 3000
		humRP:ApplyImpulse(humRP.CFrame.LookVector * math.abs(ForceMulti) * -1 + Vector3.new(0, 0, 0))

		print("Sforce")
	elseif actionName == "DashRight" and inputState == Enum.UserInputState.Begin then
		local ForceMulti = 3000
		humRP:ApplyImpulse(humRP.CFrame.RightVector * ForceMulti + Vector3.new(0, 0, 0))

		print("Dforce")
	end
end

uis.InputBegan:Connect(function(input:InputObject, gameProcessedEvent:boolean)
	if gameProcessedEvent then return end
	
	cas:BindActionAtPriority("DashNeutral", Dash, false, 0,Enum.KeyCode.Q)
	if input.KeyCode == Enum.KeyCode.W then
		cas:BindActionAtPriority("DashForwards", Dash, false, 0,Enum.KeyCode.Q)
	end
	if input.KeyCode == Enum.KeyCode.A then
		cas:BindActionAtPriority("DashLeft", Dash, false, 0,Enum.KeyCode.Q)
	end
	if input.KeyCode == Enum.KeyCode.S then
		cas:BindActionAtPriority("DashBackwards", Dash, false, 0,Enum.KeyCode.Q)
	end
	if input.KeyCode == Enum.KeyCode.D then
		cas:BindActionAtPriority("DashRight", Dash, false, 0,Enum.KeyCode.Q)
	end
end)

The problem seems to be that your code gets an input and then makes a connection to capture input (but it already happened). This would cause the next input to trigger the last input’s connection.

What are you hoping for to happen?

If you want the player to have to hold q then select a direction:

  • Call BindActionAtPriority for all your dash types.
  • Add a debounce to the Dash function to check if the q key is being held (if it’s not just return to end the function). (You can use GetKeysPressed to tell if q is being pressed)

If you want it so that when the player presses q they dash in their last movement direction:

  • Create a connection to Input began
  • Add a debounce inside this connection to make sure input.UserInputState == Enum.UserInputState.Begin
  • If the key pressed is w, a, s, or d, set a “lastPressed” variable to the keycode
  • In the dash function, use the “lastPressed” variable to determine which direction to dash in
1 Like

when a player is holding a direction button and press q, they dash in that direction, if no movement key is held, then they do dash neutral, which will be changed when i implement iframes

i js want to make sure that if ur already moving in one direction u still dash

Oh I see. In your dash function, instead of checking actionName == "DashForwards", check for the respective keycode inside UIS:GetKeysPressed. If all of the if statements for w, a, s, and d don’t happen, then do the neutral dash.

So TL;DR:
Have your bottom uis.InputBegan connection but just always call dash inside it and inside dash check which key is being held.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.