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)