Hello, I basically made a script that by holding w key makes the player sprint, but there are some bugs in it, and I have been struggling on how to fix them, for example, one bug is when you press the escape key to pause the game, even when you press the key to open chat while player is running, the animation and sound still playing, and and then glitches out, and don’t stop until player dies, can someone please check the script and fix it?
Here is the script:
local UIS = game:GetService('UserInputService')
local Player = game.Players.LocalPlayer
local Character = Player.Character
local maxSpeed = 32 -- The maximum speed you want to reach
local maxJump = 35 -- The maximum speed you want to reach
local acceleration = 0.03 -- The rate at which the speed increases
local currentSpeed = 16
local currentJump = 20
local currentAnimSpeed = 0.4
local currentAnimWeight = 0.5
local isAccelerating = false
local isRunning = false
local runSound = game.SoundService.Running
runSound.PlaybackSpeed = currentSpeed / maxSpeed -- Adjust playback speed initially
local function increaseSpeed()
if currentSpeed < maxSpeed then
currentSpeed = currentSpeed + acceleration
currentAnimSpeed = currentAnimSpeed + (acceleration / maxSpeed)
currentAnimWeight = currentAnimWeight + (acceleration / maxSpeed)
else
currentSpeed = maxSpeed
currentAnimSpeed = 1.1
currentAnimWeight = 1.2
isAccelerating = false
end
Character.Humanoid.WalkSpeed = currentSpeed
Character.Humanoid.JumpPower = currentSpeed * 1.18 -- Increase jump power with speed
PlayAnim:AdjustSpeed(currentAnimSpeed)
PlayAnim:AdjustWeight(currentAnimWeight)
runSound.PlaybackSpeed = currentSpeed / maxSpeed -- Adjust playback speed based on current speed
end
local function increaseJump()
if currentJump < maxJump then
currentJump = currentJump + acceleration
else
currentJump = maxJump
currentAnimSpeed = 1.1
currentAnimWeight = 1.2
isAccelerating = false
end
Character.Humanoid.JumpPower = currentJump
end
function ToggleAnimationAndSound(shouldPlay)
if shouldPlay then
if not PlayAnim.IsPlaying then
PlayAnim:Play()
end
if not runSound.IsPlaying then
runSound:Play()
end
else
PlayAnim:Stop()
runSound:Stop()
end
end
local leftControlDown = false
UIS.InputBegan:Connect(function(Key,IsTyping)
if IsTyping then return end
if Key.KeyCode == Enum.KeyCode.W then
isRunning = true
isAccelerating = true
local Anim = script.SprintAnim
PlayAnim = Character.Humanoid:LoadAnimation(Anim)
ToggleAnimationAndSound(true)
print('Running')
elseif IsTyping.KeyCode == Enum.KeyCode.Escape or Enum.KeyCode.Slash then
isRunning = false
end
end)
UIS.InputEnded:Connect(function(Key,IsTyping)
if IsTyping then return end
if Key.KeyCode == Enum.KeyCode.W then
Character.Humanoid.WalkSpeed = 16
Character.Humanoid.JumpPower = 35
ToggleAnimationAndSound(false)
currentSpeed = 13
currentAnimSpeed = 0.4
currentAnimWeight = 0.8
isAccelerating = false
isRunning = false
-- print('Running stopped')
elseif Key.KeyCode == Enum.KeyCode.Escape then
isRunning = false
end
end)
function ToggleRunningIfAirborne()
if Character.Humanoid.FloorMaterial == Enum.Material.Air or leftControlDown then
ToggleAnimationAndSound(false)
elseif isRunning then
ToggleAnimationAndSound(true)
end
end
game:GetService('RunService').Heartbeat:Connect(function()
ToggleRunningIfAirborne()
if isAccelerating then
increaseSpeed()
end
end)