Script Won't Work: Toggle Sprint/Walk Feature Not Working

I want to make the script work so that if you Double Press W, A, S, or D key in a certain interval of time (0.5), it enables sprinting, changing the animation ID, and the walkspeed. the idle and walking animations and walkspeed work, but the toggle doesnt work entirely. just looking for help to fix this

local Players = game:GetService("Players")

-- Constants
local DOUBLE_TAP_INTERVAL = 0.5 -- Maximum time between key presses to count as a double tap
local sprinting = false -- Current movement mode

-- Variables to track key presses
local lastKeyPress = nil -- Last key press
local lastKeyPressTime = 0 -- Time of the last key press

local function toggleSprint()
	sprinting = not sprinting
	local humanoid = game.Players.LocalPlayer.Character:FindFirstChildOfClass("Humanoid")
	if sprinting then
		print("Sprinting enabled")
		humanoid.WalkSpeed = 17 -- Set the walk speed to 17 when sprinting
		-- Set run animation here
		game.Players.LocalPlayer.Character.Animate.run.RunAnim.AnimationId = "http://www.roblox.com/asset/?id=13802391764"
	else
		print("Walking enabled")
		humanoid.WalkSpeed = 9 -- Set the walk speed back to 9 when walking
		-- Set walk animation here
		game.Players.LocalPlayer.Character.Animate.walk.WalkAnim.AnimationId = "http://www.roblox.com/asset/?id=13770584262"
	end
end

local function onKeyPress(input)
	local currentTime = tick()
	local key = input.KeyCode

	if key == lastKeyPress and (currentTime - lastKeyPressTime) <= DOUBLE_TAP_INTERVAL then
		toggleSprint()
	end

	lastKeyPress = key
	lastKeyPressTime = currentTime
end

-- Register the key press event
game:GetService("UserInputService").InputBegan:Connect(onKeyPress)

-- Idle Animation
local function onCharacterAdded(character)
	local humanoid = character:WaitForChild("Humanoid")

	for _, playingTrack in pairs(humanoid:GetPlayingAnimationTracks()) do
		playingTrack:Stop()
	end

	local animateScript = character:WaitForChild("Animate")

	animateScript.idle.Animation1.AnimationId = "rbxassetid://13770407671"
end

local function onPlayerAdded(player)
	if player.Character then
		onCharacterAdded(player.Character)
	end
	player.CharacterAdded:Connect(onCharacterAdded)
end

Players.PlayerAdded:Connect(onPlayerAdded)

-- Walk Animation
game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		player.Character.Animate.walk.WalkAnim.AnimationId = "http://www.roblox.com/asset/?id=13770584262"
	end)
end)

-- Run Animation
game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		player.Character.Animate.run.RunAnim.AnimationId = "http://www.roblox.com/asset/?id=13802391764"
	end)
end)
2 Likes

The WalkSpeed doesnt go back to normal i presume, right? I would’ve set Enums of KeyCode’s in a table and check if input is whitelisted with table.find(), so that other buttons do not trigger the sprinting function. A script i created for you as an example:

local inputtable={Enum.KeyCode.W,Enum.KeyCode.S,Enum.KeyCode.A,Enum.KeyCode.D}
local inputrecord={}

game:GetService'UserInputService'.InputEnded:Connect(function(input)
if table.find(inputtable,input.KeyCode,1) and table.find(inputrecord,lastKeyPress,1) and input.KeyCode == lastKeyPress then
toggleSprint()
end
end)

record inputs in a table to easily access sprinting and random other abilities or whatever you want there! :slightly_smiling_face:

2 Likes

i didnt even think of that. ive been just stuck on enabling the sprinting to start. the idle and walking animations work its just the input wont change the speed to sprinting speed or to the sprinting animation in general