I have a sprint script that i modified to make it change the walk animation. It works, but it only works when the walk/run animation is reloaded. For example, when running would be turned on while walking, the walking animation stays until you reload it; you can reload it by playing another animation like jumping or idle animation. How do you fix this?

local mouse = game.Players.LocalPlayer:GetMouse()
local running = false
local humanoid = game.Players.LocalPlayer.Character.Humanoid




function getTool()	
	for _, kid in ipairs(script.Parent:GetChildren()) do
		if kid.className == "Tool" then return kid end
	end
	return nil
end


mouse.KeyDown:connect(function (key) -- Run function
	key = string.lower(key)
	if string.byte(key) == 48 then
		running = true
		local keyConnection = mouse.KeyUp:connect(function (key)
			if string.byte(key) == 48 then
				running = false
			end
		end)
		game.Players.LocalPlayer.Character.Humanoid.WalkSpeed = 20
		game.Players.LocalPlayer.Character.Animate.walk.WalkAnim.AnimationId = "rbxassetid://981305988"
		game.Players.LocalPlayer.Character.Animate.run.RunAnim.AnimationId = "rbxassetid://981305988"
		
		repeat wait () until running == false
		keyConnection:disconnect()
		game.Players.LocalPlayer.Character.Humanoid.WalkSpeed = 12
		game.Players.LocalPlayer.Character.Animate.walk.WalkAnim.AnimationId = "rbxassetid://978520563"
		game.Players.LocalPlayer.Character.Animate.run.RunAnim.AnimationId = "rbxassetid://978520563"


	end
end)