Double tapping 'WASD' to sprint

So, I have a question (not sure if this is the right category sorry if it isn’t), but how would I go about detecting if somebody double tapped ‘WASD’ ? Because right now I’m stuck.

1 Like
repeat task.wait() until game.Players.LocalPlayer.Character and game.Players.LocalPlayer.Character:FindFirstChild("HumanoidRootPart")

local Camera = game.Workspace.CurrentCamera
local Player = game.Players.LocalPlayer
local Character = Player.Character
local Humanoid = Character:FindFirstChildWhichIsA("Humanoid")

local UserInputService = game:GetService("UserInputService")
local StarterPlayer = game:GetService("StarterPlayer")
local TweenService = game:GetService("TweenService")

local DefaultFieldOfView = 70
local DefaultWalkSpeed = 16

local SprintingFieldOfView = DefaultFieldOfView * 1.25
local SprintingWalkSpeed = DefaultWalkSpeed * 1.5

local Walking = false
local Sprinting = false

local TweeningTime = 0.3

local LastTap = 0

Camera.FieldOfView = DefaultFieldOfView
Humanoid.WalkSpeed = DefaultWalkSpeed

UserInputService.InputBegan:Connect(function(Input)
	if Input.KeyCode == Enum.KeyCode.W then
		if (tick() - LastTap) < 0.1 then
			Sprinting = true
			
			LastTap = tick()
			
			TweenService:Create(Camera, TweenInfo.new(TweeningTime), {FieldOfView = SprintingFieldOfView}):Play()
			Humanoid.WalkSpeed = SprintingWalkSpeed
		end
	end
end)

UserInputService.InputEnded:Connect(function(Input)
	if Input.KeyCode == Enum.KeyCode.W then
		if Sprinting then
			Sprinting = false
			
			TweenService:Create(Camera, TweenInfo.new(TweeningTime), {FieldOfView = DefaultFieldOfView}):Play()
			Humanoid.WalkSpeed = DefaultWalkSpeed
		end
	end
end)

aeaeaeae are you talking about that minecraft sprint feature thingy?

2 Likes

He just gave you the script lol.

Implement A, S, D Support.

2 Likes

Here’s another implementation, use either solution. (for W only)

local UIS = game:GetService("UserInputService")
local Players = game:GetService("Players")
local Plr = Players.LocalPlayer
local Char = Plr.Character or Plr.CharacterAdded:Wait()
local Humanoid = Char:FindFirstChildWhichIsA("Humanoid")
local DefaultWS = Humanoid.WalkSpeed

local W_Amount = 0

function Run()
	-- // enter whatever code you used to handle running
	-- this is just test code
	Humanoid.WalkSpeed = 24
end

function RunEnd()
	-- // enter whatever code you used to handle running
	-- this is just test code
	Humanoid.WalkSpeed = DefaultWS
end

UIS.InputBegan:Connect(function(key)
	if key.KeyCode == Enum.KeyCode.W then
		W_Amount += 1
		if W_Amount >= 2 then
			Run()
		end
		task.wait(0.5)
		W_Amount = 0
	end
end)

UIS.InputEnded:Connect(function(key)
	if key.KeyCode == Enum.KeyCode.W then
		RunEnd()
	end
end)
1 Like
repeat task.wait() until game.Players.LocalPlayer.Character and game.Players.LocalPlayer.Character:FindFirstChild("HumanoidRootPart")

local Camera = game.Workspace.CurrentCamera
local Player = game.Players.LocalPlayer
local Character = Player.Character
local Humanoid = Character:FindFirstChildWhichIsA("Humanoid")

local UserInputService = game:GetService("UserInputService")
local StarterPlayer = game:GetService("StarterPlayer")
local TweenService = game:GetService("TweenService")

local DefaultFieldOfView = 70
local DefaultWalkSpeed = 16

local SprintingFieldOfView = DefaultFieldOfView * 1.25
local SprintingWalkSpeed = DefaultWalkSpeed * 1.5

local Walking = false
local Sprinting = false

local TweeningTime = 0.3

local LastTaps = {
	W = tick(),
	A = tick(),
	S = tick(),
	D = tick()
}

Camera.FieldOfView = DefaultFieldOfView
Humanoid.WalkSpeed = DefaultWalkSpeed

UserInputService.InputBegan:Connect(function(Input)
	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
		if LastTaps[Input.KeyCode.Name] then
			local TimeDistance = tick() - LastTaps[Input.KeyCode.Name]

			LastTaps[Input.KeyCode.Name] = tick()

			if TimeDistance <= 0.2 and not Sprinting then
				Sprinting = true

				TweenService:Create(Camera, TweenInfo.new(TweeningTime), {FieldOfView = SprintingFieldOfView}):Play()
				Humanoid.WalkSpeed = SprintingWalkSpeed
			end
		end
	end
end)

UserInputService.InputEnded:Connect(function(Input)
	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
		if Sprinting then
			Sprinting = false

			TweenService:Create(Camera, TweenInfo.new(TweeningTime), {FieldOfView = DefaultFieldOfView}):Play()
			Humanoid.WalkSpeed = DefaultWalkSpeed
		end
	end
end)

it works on both 4 keys now

oh no I hope this post doesn’t get deleted I spent 24 minutes on this

3 Likes

Please mark the post that solved it as the Solution.