How to detect if the player is pressing w, a, s, or d?

I have a dash system in my game when you press q. I want it to dash front back left or right depending on if the player is pressing w, a, s, or d. I don’t know how to detect which one the player is holding down. If anyone could help me I would appreciate it!

local UIS = game:GetService("UserInputService")
local Remotes = ReplicatedStorage.Network.Remotes.Events
local candash = true

UIS.InputBegan:Connect(function(i,t)
	if t then return end
	if i.KeyCode == Enum.KeyCode.Q then
		if not candash then return end
		print(CurrentDirection)
		local Data = {
			["Action"] = "Dash",
			["Direction"] = CurrentDirection
		}
		Remotes.MainCombat:FireServer(Data)
		candash = false
		task.wait(1)
		candash = true
	end
end)
2 Likes

You can use UserInputService:IsKeyDown() in order to check if certain keys (e.g. WASD) are down. Example:

local UIS = game:GetService("UserInputService")
local Remotes = ReplicatedStorage.Network.Remotes.Events
local candash = true

UIS.InputBegan:Connect(function(i,t)
	if t then return end
	if i.KeyCode == Enum.KeyCode.Q then
		if not candash then return end
		print(CurrentDirection)
		
		-- Check if W, A, S, or D is pressed.
		
		if UIS:IsKeyDown(Enum.KeyCode.W) then
			-- W is down
		elseif UIS:IsKeyDown(Enum.KeyCode.S) then
			-- S is down
		elseif UIS:IsKeyDown(Enum.KeyCode.A) then
			-- A is down
		elseif UIS:IsKeyDown(Enum.KeyCode.D) then
			-- D is down
		end
		
		local Data = {
			["Action"] = "Dash",
			["Direction"] = CurrentDirection
		}
		Remotes.MainCombat:FireServer(Data)
		candash = false
		task.wait(1)
		candash = true
	end
end)

A side note, not really relevant to your question, but whenever I make a dash system I use the Humanoid’s MoveDirection to make it more specific, however it’s completely up to you. Good luck.

2 Likes

If you have a way to speed them up “dash” the WASD keys already turn the player whatever way.
Guess you just need to add the speed when they press Q for a moment.

1 Like

I figured it out using this logic before I saw your post @2112Jay

UIS.InputBegan:Connect(function(i,t)
	if t then return end
	if i.KeyCode == Enum.KeyCode.Q then
		if not candash then return end
		print(CurrentDirection)
		local Data = {
			["Action"] = "Dash",
			["Direction"] = CurrentDirection
		}
		Remotes.MainCombat:FireServer(Data)
		candash = false
		task.wait(1)
		candash = true
	end
end)
game.UserInputService.InputBegan:Connect(function(input,t)
	if t then return end
	if input.KeyCode == Enum.KeyCode.W or input.KeyCode == Enum.KeyCode.A or input.KeyCode == Enum.KeyCode.S or input.KeyCode == Enum.KeyCode.D  then
		Fourth = Third
		Third = Second
		Second = First
		First = input.KeyCode
	end
	CurrentDirection = First
end)
game.UserInputService.InputEnded:Connect(function(input)
	if input.KeyCode ~= Enum.KeyCode.W and input.KeyCode ~= Enum.KeyCode.A and input.KeyCode ~= Enum.KeyCode.S and input.KeyCode ~= Enum.KeyCode.D then print("hi") return end
	if input.KeyCode == First then
		First = Second
		Second = Third
		Third = Fourth
		Fourth = nil
	elseif input.KeyCode == Second then
		Second = Third
		Third = Fourth
		Fourth = nil
	elseif input.KeyCode == Third then
		Third = Fourth
		Fourth = nil
	elseif input.KeyCode == Fourth then
		Fourth = nil
		
	end
	CurrentDirection = First
end)

Thanks for helping though!

4 Likes