In a directional dashing script, how do I differentiate between keys being held and pressed while the player sprints?

Hi, this is a movement script which has sprinting + dashes, so the player would likely be holding w constantly, while making adjustments with A & D, but also pressing Q to dash. Here’s the function which handles the dashing (the scripting isn’t relevant here, i think.)

local Keybind = Enum.KeyCode.Q

local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")

local UIS = game:GetService("UserInputService")

local DashTime = 1.2
local Force = 12.5 + Player.Character.CharacterData.SpeedLevel.Value
local dashcd = false
local dashdir = nil
local timeheld = 0
local keyheld = false

UIS.InputBegan:Connect(function(input, gpe)
	if gpe then return end
	if dashcd == false then
				if Player.Character:WaitForChild("PlayerTags").Stunned.Value == true then return end
						print("hi1")
						local animW = Humanoid:LoadAnimation(game.ReplicatedStorage.Animations.ForwardRoll)
						local animA = Humanoid:LoadAnimation(game.ReplicatedStorage.Animations.LeftRoll)
						local animS = Humanoid:LoadAnimation(game.ReplicatedStorage.Animations.BackwardRoll)
						local animD = Humanoid:LoadAnimation(game.ReplicatedStorage.Animations.RightRoll)
						if input.KeyCode == Enum.KeyCode.W then
							dashdir = "Forwards"
						elseif input.KeyCode == Enum.KeyCode.D then
							dashdir = "Right"
						elseif input.KeyCode == Enum.KeyCode.A then
							dashdir = "Left"
						elseif input.KeyCode == Enum.KeyCode.S then
							dashdir = "Back"	
						elseif input.KeyCode == Keybind then
							dashcd = true
							local Slide = Instance.new("BodyVelocity")
							Slide.MaxForce = Vector3.new(1,1,1) * 20000
							Slide.Velocity = Humanoid.MoveDirection * Force
							Slide.Parent = Character.HumanoidRootPart
							print("hi")
							if dashdir == "Forwards" then
								animW:Play()
							elseif dashdir == "Left" then
								animA:Play()
							elseif dashdir == "Right" then
								animD:Play()
							elseif dashdir == "Back" then
								animS:Play()
							end
							wait(DashTime)
							game.Debris:AddItem(Slide, 0.1)	
							task.wait(5)
							dashcd = false
						end
	end
end)

thanks!!

1 Like

yo id appreciate a response please im still having trouble thx

I think this dev forum post might answer your question.

it does help, but the issue is that W will always be held, and A/D will be used for adjustments, the script i have just works based on the last key pressed, but since w is always held i dont know how to differentiate between the two